开发者

SQL Select a row and store in a SQL variable

So, I'm writing this Stored Proc and I really suck at SQL.

My Question to you guys is:

Can I select an entire row and store it in a variable?

开发者_高级运维

I know I can do something like:

declare @someInteger int
select @someInteger = (select someintfield from sometable where somecondition)

But can I select the entire row from sometable and store it in a variable?


You can select the fields into multiple variables:

DECLARE @A int, @B int

SELECT
  @A = Col1,
  @B = Col2
FROM SomeTable
WHERE ...

Another, potentially better, approach would be to use a table variable:

DECLARE @T TABLE (
  A int,
  B int
)
INSERT INTO @T ( A, B )
SELECT
  Col1,
  Col2
FROM SomeTable
WHERE ...

You can then select from your table variable like a regular table.


You could create a table variable that matches your table schema and store the single row in it:

declare @myrow table(field0 int,field1 varchar(255))
insert into @myrow
select field0,field1 from mytable where field0=1


Please see/via:

MSSQL Select statement with incremental integer column... not from a table

SELECT ROW_NUMBER() OVER( ORDER BY Column1, Column2 ) AS 'rownumber',*
FROM YourTable
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜