0

Here is my scenario, I'm creating a dynamic query using a select statement which uses functions to generate the query. I am storing it into a variable and running it using exec. i.e.

declare @dsql nvarchar(max)
set @dsql = ''

select @dsql = @dsql + dbo.getDynmicQuery(column1, column2)
from Table1

exec(@dsql)

Now it produces the many errors in this scenario, like 'Incorrect syntax near ','' and 'Case expressions may only be nested to level 10.'

But if i take the text from @dsql and assign it a variable manually like:

declare @dsql nvarchar(max)
set @dsql = ''

set @dsql = N'<Dynamic query text>'
exec(@dsql)

it runs and generates the result, what could be the reason for that ??

Thanks..

waheed
  • 533
  • 5
  • 15
  • 3
    are you 100% sure your function generates correct SQL syntax? – Chris Bednarski May 08 '10 at 00:54
  • yes, because it's the same sql which is running if i paste it directly to the variable.. – waheed May 08 '10 at 00:58
  • Actually when it generated errors, i used select @dsql to try the code manually and look for errors, and i than pasted it into a varialbe, as shown in the second section of code, but it ran accurately their.. – waheed May 08 '10 at 01:00
  • 3
    Use `PRINT(@dsql)` prior to the `EXEC` call (should be using `EXEC sp_executesql` instead btw), and test that output. – OMG Ponies May 08 '10 at 01:04
  • If your SQL is really long, PRINT(@dsql) may get truncated. I have a special SP I use which will take a VARCHAR(MAX) and break it up for accurate printing of very long variables. – Cade Roux May 08 '10 at 01:22
  • RE: Cade's point `SELECT @dsql AS [processing-instruction(x)] FOR XML PATH('')` should bring it back without truncation in SSMS hopefully (From http://sqlblogcasts.com/blogs/martinbell/archive/2009/10/25/How-to-display-long-text-in-SSMS.aspx) – Martin Smith May 08 '10 at 01:43
  • @Martin Smith - Thanks! I believe that trick also addresses this question I had a little while ago which I never really found a satisfactory solution: http://stackoverflow.com/questions/2679481/ssms-results-to-grid-crlf-not-preserved-in-copy-paste-any-better-techniques – Cade Roux May 08 '10 at 01:51
  • @Cade - Glad to be of help. Haven't had a chance to test it myself yet! – Martin Smith May 08 '10 at 01:55
  • @Cade, @Martin Smith, @OMG Ponies: Thanks for the assistance, i have tried these methods, but still the same problem persists, The query runs when i copy it into a variable, but not directly when i run it after generation.. I'm trying, will post the issue, when i resovle it.. – waheed May 08 '10 at 02:05
  • @OMG Ponies: since print() had, been recommended first by you, so if you write an answer, i'll mark it as accepted one.. thanks.. – waheed May 08 '10 at 03:46

2 Answers2

1

I have resolved it, actually inside getDynmicQuery(), i am using few .Net functions for few specific parts of the query.

Now the problem was that, in the query text there was single quotes as well and i was generating them in a standard SQL way, i.e. two single quotes wherever i needed one single quote in the query text. It was OK, for UDF, but for .Net function that created problem.

Because the query generated by .Net function was simple text and i just concatenated it with another variable to get one complete query so escaping single quote inside .Net function was not required.

whenever i used select it was escaping so query was ok, same was the case with SELECT @dsql AS [processing-instruction(x)] FOR XML PATH(''), print() had helped me to figure this out, since it didn't escaped anything, but problem with it was, that truncated my query, so i have try a specific shorter version of query for testing..

Edit, A little more detail:

Since i'm generating scripts in two steps,

  1. Inside a UDF
  2. .Net function

whole issue is of single quotes, Initially inside UDF and .Net function where i needed one single quotes in generated query i used two single quotes in dynamic query text(one for escaping).

The correction that i had done, is that in .Net function, where i needed single quote in generated query, i am now using single quote, no escaping.

Now whenever in old scenario, i used select to select the dynamic query text, single quote escaping was done, so i didn't received the exact query, what print did is that, it printed without doing any escaping, which made it possible to see the error.

Thanks Everybody..

waheed
  • 533
  • 5
  • 15
  • Glad you got it figured out can you just explain the cause a bit more though as I don't quite follow and it sounds a good thing to be aware of. I can't see any difference between `DECLARE @X varchar(10) SET @X = '''TEST''' PRINT @X SELECT @X AS [processing-instruction(x)] FOR XML PATH('')` Is it something specific to strings coming back from the CLR? – Martin Smith May 08 '10 at 09:42
1

Use:

PRINT(@dsql) 

...prior to the EXEC call (should be using EXEC sp_executesql instead btw), and test the output from the print to see where the syntax error is coming from.

OMG Ponies
  • 325,700
  • 82
  • 523
  • 502