0

What I want to do is to get the “value result” of the query in a variable @statement.

the code looks as follows:

declare @ID int, @name varchar (200), @statement varchar (200)

set @ID = 25
set @name = 'select FirstName + '' '' + LastName as ''Full Name''
             from person.person  
             where BusinessEntityID = ' + cast (@ID as varchar)'
set @statement = 'My name is @name'

I'd like to show the results from @statement, and I know i should use sp_executesql to execute my @name query, and output it @statement, but I am not sure how to do that.

Any thoughts and thanks!

2 Answers 2

3

A little confused. No need for the dynamic SQL

Example

declare @ID int, @name varchar (200)
set @id=25

set @name = 'My name is ' + (select FirstName + ' ' + LastName 
                               from person.person  
                               where BusinessEntityID = @ID
                             )
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, but I'd like the output to be a statement as 'My name is xxxx', that's why I am considering the 3rd variable @statement...
@Kay Updated :)
0

You can use concat() :

select top (1) @name = concat('My name is ', FirstName, ' ', LastName)
from person.person  
where BusinessEntityID = @ID;

I would use to top (1) if BusinessEntityID matching pair has multi row.

Comments

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.