Quantcast
Channel: SQLServerCentral » SQL Server 2014 » Development - SQL Server 2014 » Latest topics
Viewing all articles
Browse latest Browse all 3145

sp_executesql, dynamic sql, and openrowset: parameters not being replaced by values in

$
0
0
Ok, I have some stored procedures that are using EXEC and I am updating them to use sp_executesql instead.In the same proc, I've tested just a straight simple sql command with one param inside the where clause like this:DECLARE @param NCHAR(1) = 6DECLARE @sqlTest NVARCHAR(max) = 'SELECT * from IOErrors WHERE VENDORNO = @testParam'EXEC sp_executesql @sqlTest, N'@testParam NCHAR(1)', @testParam = @paramThis works just fine. So, I know the debugger is functioning properly. (I had to reboot from a problem with the debugger, so that's solved)But in the actual stored procedure logic, it is doing an insert into a temptable from a local file system txt file via OPENROWSET like this:DECLARE @sql NVARCHAR(MAX) = N' INSERT INTO #custom_ImportWebContent' + CHAR(13) + CHAR(10) + N' SELECT I.[ItemId], REPLACE(ORS.[ProductId],CHAR(34),''''), REPLACE(ORS.[SKUNo],CHAR(34),''''), REPLACE(ORS.[Title],CHAR(34),''''), REPLACE(ORS.[ImageFile],CHAR(34),'''') ' + CHAR(13) + CHAR(10) + N' FROM OPENROWSET(' + CHAR(13) + CHAR(10) + N' BULK ''' + @LocalSQLFilePath + '''' + CHAR(13) + CHAR(10) + N' , FORMATFILE = ''' + @LocalSQLFmtFilePath + '''' + CHAR(13) + CHAR(10)IF ISNULL(@FirstRow,0) > 0 SET @sql = @sql + N' , FIRSTROW = [highlight=#ffff11]@paramFirstRow [/highlight]' + CHAR(13) + CHAR(10)IF ISNULL(@LastRow,0) > 0 SET @sql = @sql + N' , LASTROW = [highlight=#ffff11]@paramLastRow[/highlight] , ROWS_PER_BATCH = CAST(@paramLastRow AS NVARCHAR(10)) ' + CHAR(13) + CHAR(10)In the above code, @LocalSQLFilePath and @LocalSQLFmtFilePath are 'outside the single quotes' and are treated as regular params to concatenate into the dynamic SQL...However, and this is the big one, I modified it so that @paramFirstRow and @paramLastRow are 'inside the single quotes' and are treated as just part of the string, to later be replaced in the call to sp_executesql like this:DECLARE @FirstRowCHAR NVARCHAR(10) = CAST(@FirstRow as NVARCHAR(10)) -- these come in as INT so I need to cast to NVARCHAR for use in dynamic sql string.. don't I?DECLARE @LastRowCHAR NVARCHAR(10) = CAST(@LastRow as NVARCHAR(10))EXEC sp_executesql @sql, N'@paramFirstRow NVARCHAR(10), @paramLastRow NVARCHAR(10)', @paramFirstRow = @FirstRowCHAR, @paramLastRow = @LastRowCHAR Just as in my first example at the top, this last line should work the same, correct?FYI, I already know that one MUST use dynamic SQL when trying to use a param in OPENROWSET, I know that.. which is why this proc was written to use dynamic sql in the first place. The error I am getting suggests that the params are not being replaced. Here's the error:"Incorrect syntax near '@paramFirstRow'"... is there some trick for this I am missing? Or does OPENROWSET just not work with sp_executesql and param replacement and must just use dynamic sql only?...Thanks for your time.

Viewing all articles
Browse latest Browse all 3145

Trending Articles