Declare @ date_pa1 datetime, @ date_pa2 datetime
Declare @ I int
The set @ date_pa1='2020-01-10'
The set @ date_pa2='2020-01-15'
The set @ I=1
While (@ i
=2)The begin
The set @ sql_pa='@ date_pa + ltrim (STR (@ I))
Print (@ sql_pa)
The set @ I=@ I + 1
End
The output of the implementation:
@ date_pa1
@ date_pa2
I want to ask now @ sql_pa variable current value for this assignment '@ date_pa1', how to implement the print (@ date_pa1) results? Try the EXEC (no)
Want to get the following results:
Jan 10, 2020 12:00 AM
Jan 15, 2020 12:00 AM
CodePudding user response:
Want to get the following results:Jan 10, 2020 12:00 AM
Jan 15, 2020 12:00 AM
Is this how to modify the print (@ sql_pa)?
CodePudding user response:
Declare @ sql_pa VARCHAR (Max)Declare @ date_pa11 datetime, @ date_pa22 datetime
The set @ date_pa11='2020-01-10'
The set @ date_pa22='2020-01-15'
Declare @ I int
The set @ I=1
While (@ i
=2)The begin
Set @ sql_pa='declare @ date_pa1 datetime, @ date_pa2 datetime' + 'set @ date_pa1=' "+ cast (@ date_pa11 as varchar (50)) +" '"' + 'set @ date_pa2=' "+ cast (@ date_pa22 as varchar (50)) +" '"'
The set @ sql_pa=@ sql_pa + 'select @ date_pa + ltrim (STR (@ I))
Print (@ sql_pa)
The exec (@ sql_pa)
The set @ I=@ I + 1
End
Can go to the exec
CodePudding user response:
But this can only be in the results will be displayed to displayed in the message
CodePudding user response:
I just want this @ sql_pa corresponding @ date_pa1 in WHILE using the specified value, the exec is too long.CodePudding user response:
DECLARE @ sql_pa VARCHAR (20)
DECLARE @ SQL NVARCHAR (100)
DECLARE @ DATETIME x
DECLARE @ date_pa1 DATETIME, @ date_pa2 DATETIME
DECLARE @ I INT
The SET @ date_pa1='2020-01-10'
The SET @ date_pa2='2020-01-15'
The SET @ I=1;
WHILE (@ I & lt;
=2)The BEGIN
The SET @ sql_pa='@ date_pa + LTRIM (STR (@ I))
The SET @ SQL='SELECT @ x=' + @ sql_pa
The EXEC sp_executesql @ SQL, N '@ date_pa1 datetime, @ date_pa2 datetime, @ x datetime OUTPUT', @ date_pa1, @ date_pa2, @ x OUTPUT
PRINT @ x
The SET @ I=@ I + 1
END
CodePudding user response:
Declare @ sql_pa VARCHAR (Max)Declare @ date_pa11 datetime, @ date_pa22 datetime
The set @ date_pa11='2020-01-10'
The set @ date_pa22='2020-01-15'
Declare @ I int
The set @ I=1
While (@ i
=2)The begin
- declare @ XXXX datetime
Set @ sql_pa='declare @ XXXX datetime, @ date_pa1 datetime, @ date_pa2 datetime' + 'set @ date_pa1=' "+ cast (@ date_pa11 as varchar (50)) +" '"' + 'set @ date_pa2=' "+ cast (@ date_pa22 as varchar (50)) +" '"'
The set @ sql_pa=@ sql_pa + 'set @ XXXX=(select @ date_pa' + ltrim (STR (@ I)) + ') print (@ XXXX) '
- print (@ sql_pa)
The exec (@ sql_pa)
- print (@ XXXX)
The set @ I=@ I + 1
End
This can be a
CodePudding user response:
CodePudding user response:
There is no easy way, I just want to @ sql_pa this value, pointing to the variable @ date_pa1 ~ N the corresponding value