December 5, 2013 at 7:53 am
hi (me again)
i'm trying to get a proc to execute by passing in a dynamic sql string.
this works...
exec [MediaCreateCustomerDataSet] @accountId, @yr, @stockGroup, @stockCode, @stockDescription
but this doesn't (error: The name '[MediaCreateCustomerDataSet] @accountId, @yr, @stockGroup, @stockCode, @stockDescription' is not a valid identifier.)...
set @sqlStr = @dataSet + ' @accountId, @yr, @stockGroup, @stockCode, @stockDescription'
exec @sqlStr
i also tried (error: Incorrect syntax near ',')...
set @sqlStr = @dataSet + ' ' + @accountId, @yr, @stockGroup, @stockCode, @stockDescription
exec @sqlStr
thanks
December 5, 2013 at 2:20 pm
Why the square brackets around [MediaCreateCustomerDataSet] ?
They aren't generally needed unless you're using a SQL Server reserved word or have spaces in the name. Have you tried your dynamic query without them?
Sigerson
"No pressure, no diamonds." - Thomas Carlyle
December 5, 2013 at 2:28 pm
December 5, 2013 at 2:36 pm
This way be an object name, and optionally parameters after object name
exec @sqlStr
THIS way, you can execute a string command: note the parenthesis!
exec(@sqlStr )
Lowell
December 6, 2013 at 6:48 am
Lowell,
Good catch! Something bothered me about that EXEC statement but I didn't stop to think about it. An idle question for anybody: are those brackets a problem or not?
Sigerson
"No pressure, no diamonds." - Thomas Carlyle
December 6, 2013 at 6:56 am
Sigerson (12/6/2013)
Lowell,are those brackets a problem or not?
Nope, no problem.
If you script anything or use the shortcut menu to execute a procedure in SSMS it puts brackets around everything.
Far away is close at hand in the images of elsewhere.
Anon.
December 6, 2013 at 7:30 am
the (@sqlStr) seems to have worked, thanks
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply