Solved

Help needed to execute dynamic sql with sp_executesql

Posted on 2008-10-17
4
625 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

726 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