Solved

Help needed to execute dynamic sql with sp_executesql

Posted on 2008-10-17
4
622 Views
Last Modified: 2012-05-05
Spent far too long on this....

I need to create a view in data base b in the context of database a

so I tried...
use database_a
go

DECLARE @client nvarchar(100)
DECLARE @QrySql nvarchar(1000)

SELECT @client=N'database_b'
SET @QrySql =N'CREATE view ' + @client + '.[dbo].[vw_bcp_xml] AS SELECT      ''bcp '' + TABLE_CATALOG + ''.dbo.'' + TABLE_NAME + '' format nul -x -f c:\reporting\bcp\'' + TABLE_CATALOG + ''\'' + TABLE_NAME + ''.xml -n -U reporting -P letmein -S db1\reporting'' AS bcp_xml FROM INFORMATION_SCHEMA.TABLES   WHERE TABLE_NAME LIKE ''tbl_rpt%'''
PRINT @QrySql
EXEC (@QrySql)

which as you'd expect gives the error:

'CREATE/ALTER VIEW' does not allow specifying the database name as a prefix to the object name.


So i tried this....

DECLARE @client nvarchar(100)
DECLARE @QrySql nvarchar(1000)
DECLARE @QrySql2 nvarchar(1000)

SELECT @client=N'database_b'
SET @QrySql =N'CREATE view ' + @client + '.[dbo].[vw_bcp_xml] AS SELECT      ''bcp '' + TABLE_CATALOG + ''.dbo.'' + TABLE_NAME + '' format nul -x -f c:\reporting\bcp\'' + TABLE_CATALOG + ''\'' + TABLE_NAME + ''.xml -n -U reporting -P letmein -S db1\reporting'' AS bcp_xml FROM INFORMATION_SCHEMA.TABLES   WHERE TABLE_NAME LIKE ''tbl_rpt%'''

SET @QrySql2 = @Client + '.dbo.sp_executesql(' +  @QrySql + ')
EXEC (@QrySql2)

I just can't get it right as I'm getting sytax errors when executing the string.

I guess I can't quite get my head around where to put the quotes etc...When (or if!!) I get it working I want to loop round a list of clients and create this view in all client databases.

I'd appreciate help in either:

1. Fixing the code so it works
or
2. An effective alternative method.

Thanks in advance
0
Comment
Question by:alito_procurement
  • 2
  • 2
4 Comments
 
LVL 23

Expert Comment

by:adathelad
ID: 22739921
Hi,

Try this, incorporating a USE statement into the dynamic SQL.:

use database_a
go

DECLARE @client nvarchar(100)
DECLARE @QrySql nvarchar(1000)

SELECT @client=N'database_b'
SET @QrySql =N'USE ' + @client + '; CREATE view [dbo].[vw_bcp_xml] AS SELECT      ''bcp '' + TABLE_CATALOG + ''.dbo.'' + TABLE_NAME + '' format nul -x -f c:\reporting\bcp\'' + TABLE_CATALOG + ''\'' + TABLE_NAME + ''.xml -n -U reporting -P letmein -S db1\reporting'' AS bcp_xml FROM INFORMATION_SCHEMA.TABLES   WHERE TABLE_NAME LIKE ''tbl_rpt%'''
PRINT @QrySql
EXEC (@QrySql)
0
 

Author Comment

by:alito_procurement
ID: 22739959
Thought that was going to work but......

'CREATE VIEW' must be the first statement in a query batch.

Thanks anyway
0
 
LVL 23

Accepted Solution

by:
adathelad earned 250 total points
ID: 22740037
Sorry, you'll need to wrap another EXECUTE() call inside @QrySql to create the view, like this:

DECLARE @client nvarchar(100)
DECLARE @QrySql nvarchar(1000)
DECLARE @ViewSql NVARCHAR(1000)

SELECT @client=N'database_b'
SET @ViewSql = 'CREATE view [dbo].[vw_bcp_xml] AS SELECT      ''bcp '' + TABLE_CATALOG + ''.dbo.'' + TABLE_NAME + '' format nul -x -f c:\reporting\bcp\'' + TABLE_CATALOG + ''\'' + TABLE_NAME + ''.xml -n -U reporting -P letmein -S db1\reporting'' AS bcp_xml FROM INFORMATION_SCHEMA.TABLES   WHERE TABLE_NAME LIKE ''tbl_rpt%'''

SET @QrySql =N'USE ' + @client + '; EXECUTE(@ViewSql)'

EXECUTE sp_executesql @QrySql, N'@ViewSql NVARCHAR(1000)', @ViewSql


I *think* that's basically correct. The example I tested with was a simplified version:
DECLARE @sql NVARCHAR(1000)
SET @sql = 'USE DatabaseA; EXECUTE(''CREATE VIEW dbo.vwTest AS SELECT * FROM TableA'')'

EXECUTE(@sql)
0
 

Author Closing Comment

by:alito_procurement
ID: 31507098
That worked...without any amendments

nice one
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question