SQL 选择一行并存储在 SQL 变量中 [英] SQL Select a row and store in a SQL variable

查看:34
本文介绍了SQL 选择一行并存储在 SQL 变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我正在编写这个存储过程,但我真的很擅长 SQL.

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

我对你们的问题是:

我可以选择整行并将其存储在变量中吗?

我知道我可以这样做:

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

但是我可以从 sometable 中选择整行并将其存储在一个变量中吗?

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.

这篇关于SQL 选择一行并存储在 SQL 变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆