0

I've been wondering if it's possible to declare and set a variable within the definition of another variable. For example:

declare @variable varchar(250)

set @variable =

'INSERT INTO [BLAH] (Nope, Hype, Friends)
VALUES (declare @value varchar(250) set @value = 'example' exec @value, @value, @value)'

exec @variable

I'm probably doing multiple really basic mistakes here - just trying to understand as much as fast as possible

2 Answers 2

1

it can be done as below.

declare @variable varchar(800)

set @variable =

' declare @value varchar(10) = ''example''
 INSERT INTO [BLAH] (Nope, Hype, Friends)
 VALUES ( @value, @value, @value)'

exec @variable

Also get a knowledge of SQL injection before you work with dynamic sql.

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

Comments

0

Yes you can declare a set of variable inside a variable: doing like this.

DECLARE @variable varchar(max)
SET @variable =
'declare @value varchar(max) = ''example''
Insert into BLAH
values(@value,@value)'
EXEC (@variable)

Here we simply declared a variable @value inside another variable @variable

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.