0

I am using dynamic sql, to query the database:

    DECLARE @VALUE VARCHAR(5000);
    SET @VALUE = '123'
    DECLARE @SQL5 NVARCHAR(MAX) = 'Select distinct item_id, attr_val from [dbo].[CONTRACT_ATTR] WHERE [ATTR_VAL]  LIKE ''%@VALUE%'' AND [FIELD_ID] = 413 ORDER BY [attr_val]';
    SET @SQL5 = replace(@SQL5, '@VALUE', @VALUE);
    EXEC SP_executesql @SQL5;

These are the results:

enter image description here

I am trying to take the resulting (item_id) and run another select query. Something like this:

UNION
Select Column3 From @SQL5 where other_column = 1234

The results would be 3 columns for each row returned, the 2 original and the new one found in the 2nd select.

What am I doing wrong and how do I fix it?

7
  • why are you using REPLACE if you know the value? Commented Feb 19, 2017 at 22:31
  • @McNets I don't I just have the above 123 as an example. Commented Feb 19, 2017 at 22:33
  • 'Union' requires that the two select statements return same columns (schema of the output must be the same) Commented Feb 19, 2017 at 22:42
  • What relevance does your seconds query have to the first query? From what you have explained there is no need for dynamic SQL or to do this in two steps Commented Feb 19, 2017 at 22:48
  • 1
    Does the 1234 in the second query relate to the item_id in the first query? That's what a join does. Since you have three rows in the first query, I guess you are expecting possibly three or more rows as a result from your second query? It seems that you just need use a join. Commented Feb 19, 2017 at 22:59

2 Answers 2

1

Here is an answer without dynamic SQL:

DECLARE @VALUE VARCHAR(100)

SET @VALUE = '123'

SELECT myuser.item_id,
       myuser.attr_val,
       parent.attr_val
FROM [dbo].[CONTRACT_ATTR] AS myuser
JOIN [dbo].[CONTRACT_ATTR] AS parent 
  ON parent.item_id = myuser.item_id 
  AND myuser.item_id = parent.item_id 
WHERE myuser.field_id = 239 
and parent.[ATTR_VAL]  LIKE '%' + @VALUE + '%'

I also moved join conditions wholly into the on clause.

Sign up to request clarification or add additional context in comments.

Comments

0

I got it:

SELECT myuser.item_id,
       myuser.attr_val,
       parent.attr_val
FROM [dbo].[CONTRACT_ATTR] AS myuser
JOIN [dbo].[CONTRACT_ATTR] AS parent 
  ON parent.item_id = myuser.item_id 
WHERE myuser.item_id = parent.item_id and myuser.field_id = 239 and parent.[ATTR_VAL]  LIKE '%123%'

integrating dynamic sql:

    DECLARE @SQL5 NVARCHAR(MAX) = 'SELECT myuser.item_id, myuser.attr_val, parent.attr_val FROM [dbo].[CONTRACT_ATTR] AS myuser JOIN [dbo].[CONTRACT_ATTR] AS parent ON parent.item_id = myuser.item_id WHERE myuser.item_id = parent.item_id and myuser.field_id = 239 and parent.[ATTR_VAL]  LIKE ''%@VALUE%''';
    SET @SQL5 = replace(@SQL5, '@VALUE', @VALUE);
    EXEC SP_executesql @SQL5;

1 Comment

What is the point of dynamic SQL here? You can just use a variable directly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.