Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

need to assign values from sql query in single instance

Tags:

sql

sql-server

I am executing the below query in sql-server-2008-R2 which gives me output as I wanted(i.e, number of rows) but, I want to store the result in form of a multi-columned row(s) in a variable.

declare @rCount int
declare @kuri nvarchar(max)
declare @IDs nvarchar(max)

select @rCount=10

set @kuri='select top '+cast(@rCount as varchar)+' FLD295,FLD9 from tableName (nolock) ORDER BY NEWID()'

execute(@kuri)

I had done this earlier but it was different query where I need to concatenate the result of both the queries in to one column (assigning and display)

select @IDs=CAST(isnull(@IDs ,'')as varchar)+CAST(COALESCE(fld9,',')as varchar) from table1307 (nolock) ORDER BY NEWID()

1 Answers

DECLARE @T1 TABLE (
    FLD9 bigint
, FLD295 nVARCHAR(max)
)

insert into @T1(,FLD295,FLD9) 
select top(@rCount) FLD295,FLD9 from tableName (nolock)
 ORDER BY NEWID()

select * from @T1
select @IDs=CAST(isnull(@IDs ,'')as varchar)+CAST(COALESCE(fld9,',')as varchar) from @T1 
like image 102
Mir Shakeel Hussain Avatar answered Apr 11 '26 22:04

Mir Shakeel Hussain



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!