?
Solved

Help needed to execute dynamic sql with sp_executesql

Posted on 2008-10-17
4
Medium Priority
?
630 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 1000 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

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

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. …
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

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