First, I know this has been addressed before - I've done my research and as you'll see below, I have tried multiple versions to get this to work.
I am trying to set up a query in which a variable can be set and the query uses that variable. Here's my code:
-- Set to the category you want to check
DECLARE @validationCategory varchar = 'Dept'
DECLARE @validationTable varchar = (SELECT ValidationTable FROM MasterFiles..Categories WHERE Category = @validationCategory AND TableToValidate = 'xref')
DECLARE @validationField varchar = (SELECT ValidationField FROM MasterFiles..Categories WHERE Category = @validationCategory AND TableToValidate = 'xref')
EXEC('
SELECT DISTINCT Category
FROM MasterFiles.dbo.xref
WHERE Category = ''' + @validationCategory + '''
AND (new_value NOT IN (SELECT ''' + @validationField + '''
FROM ' + @validationTable + ')
AND old_value NOT LIKE ''%New%''
OR (new_value IS NULL OR new_value = ''''))
)'
)
When I run it I am getting this error:
Msg 102, Level 15, State 1, Line 6
Incorrect syntax near ')'.
The most I've been able to glean from the error is that Level 15 means the error is correctable by the user. Great! Now if I could just find out exactly what the syntax problem is...
I'm suspecting it may be due to the fact that @validationTable is a table, not a string, but I'm not sure how to address that.
After some more research I then tried to put the query in a variable and then run EXEC on the variable:
--...
DECLARE @Query nvarchar
SET @Query = '
SELECT DISTINCT Category
FROM MasterFiles.dbo.xref
WHERE Category = ''' + @validationCategory + '''
AND (new_value NOT IN (SELECT ''' + @validationField + '''
FROM ' + @validationTable + ')
AND old_value NOT LIKE ''%New%''
OR (new_value IS NULL OR new_value = ''''))
)'
EXEC(@query)
That ran without errors, but I didn't get any output - just that it ran successfully.
After some more research about getting the output of a query as an EXECuted variable, I came up with this (from an example I found here on SO):
--...
DECLARE @vQuery nvarchar
SET @vQuery = '
SELECT DISTINCT Category
FROM MasterFiles.dbo.xref
WHERE Category = ''' + @validationCategory + '''
AND (new_value NOT IN (SELECT ''' + @validationField + '''
FROM ' + @validationTable + ')
AND old_value NOT LIKE ''%New%''
OR (new_value IS NULL OR new_value = ''''))
)'
DECLARE @vi int
--DECLARE @vQuery varchar(1000)
EXEC SP_EXECUTESQL
@query = @vQuery
, @params = '@vi int output'
, @vi = @vi output
SELECT @vi
That resulted in this error:
Msg 214, Level 16, State 3, Procedure sp_executesql, Line 1
Procedure expects parameter '@parameters' of type 'ntext/nchar/nvarchar'.
So what's the correct way to go about this. In my research on this I've seen examples that use a stored procedure. I'd like to be able to do this without a stored procedure because this query will be used by copy/pasting the query into a query window in a client's SSMS, setting the table variable and running the query. Then moving on to the next client to do the same thing.
I need to be able to run this on servers installed with either SQL Server 2008 or 2012.
PRINTto see what sql you're generating.. you have a)in the wrong place somewhere