Solved

Help needed to execute dynamic sql with sp_executesql

Posted on 2008-10-17
4
613 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
That worked...without any amendments

nice one
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now