分配给变量的SQL结果? [英] SQL result assigned to a variable?

查看:91
本文介绍了分配给变量的SQL结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将SQL查询的结果分配给变量。

的结果以下声明总是会导致返回1行。


SELECT ProductDescription,ProductID

FROM FROM

WHERE(ProductIDNumber =?)

如何将...说明ProductDescription分配给名为prodDesc的变量?


谢谢,

Tony

How would I assign the result of a SQL query to a variable. The result of
the following statement will ALWAYS result in 1 row returned.

SELECT ProductDescription, ProductID
FROM Products
WHERE (ProductIDNumber = ?)

How do I assign the...say, ProductDescription to a variable called prodDesc?

Thanks,
Tony

推荐答案

tony,


我不确定你的问题是什么,


你想要这个外行的第一行而不是添加Top1

如果你想制作这个表达式的一行比添加Distinct


如果你只想要一行返回而不是使用datareader,


Cor

" Tony K" < ki ******** @ berst的NOSPAMcomcast.netschreef

新闻:好的************** @ TK2MSFTNGP03.phx.gbl .. 。
tony,

I am not sure of what your question is,

Do you want the first row of this exression than add Top1
If you want to make one row of this expression than add Distinct

If you only want one row to return than use the datareader,

Cor
"Tony K" <ki********@NOSPAMcomcast.netschreef in bericht
news:Ok**************@TK2MSFTNGP03.phx.gbl...

如何将SQL查询的结果分配给变量。

的结果以下声明总是会导致返回1行。


SELECT ProductDescription,ProductID

FROM FROM

WHERE(ProductIDNumber =?)

如何将...说明ProductDescription分配给名为

prodDesc的变量?


谢谢,

Tony
How would I assign the result of a SQL query to a variable. The result of
the following statement will ALWAYS result in 1 row returned.

SELECT ProductDescription, ProductID
FROM Products
WHERE (ProductIDNumber = ?)

How do I assign the...say, ProductDescription to a variable called
prodDesc?

Thanks,
Tony



有几种方式这样做。我假设您的意思是将从SQL查询中说出ProductDescription列的

值分配给调用查询的代码中的变量

,对吧?如果你只是在一次返回

值之后,比如ProductDescription,你可以使用Command类的执行标量方法。如果您在当前返回的单行之后,

但想要在代码中存储一个或多个返回值,那么在使用参数化查询时看看

带输出参数。


以下是一些示例, http://support.microsoft.com/kb/308049/

-

Carsten Thomsen

高级.NET解决方案架构师/开发人员/作者

MCAD / MCSD / MCSE / MCTS

" Tony K" < ki ******** @ NOSPAMcomcast.netWrote in message

news:Ok ************** @ TK2MSFTNGP03.phx.gbl .. 。
There are several ways of doing this. I am assuming that you mean assign the
value of say the ProductDescription column from the SQL query to a variable
in your code that calls the query, right? If you′re only after one return
value, such as ProductDescription, you can use the ExecuteScalar method of
the Command class. If you′re after the single row as is currently returned,
but want to store one or more of the returned values in your code, then look
at using parameterized queries with output parameters.

Here are some samples, http://support.microsoft.com/kb/308049/

--
Carsten Thomsen
Senior .NET Solutions Architect / Developer / Author
MCAD/MCSD/MCSE/MCTS
"Tony K" <ki********@NOSPAMcomcast.netwrote in message
news:Ok**************@TK2MSFTNGP03.phx.gbl...

如何将SQL查询的结果分配给变量。

的结果以下声明总是会导致返回1行。


SELECT ProductDescription,ProductID

FROM FROM

WHERE(ProductIDNumber =?)

如何将...说明ProductDescription分配给名为

prodDesc的变量?


谢谢,

Tony
How would I assign the result of a SQL query to a variable. The result of
the following statement will ALWAYS result in 1 row returned.

SELECT ProductDescription, ProductID
FROM Products
WHERE (ProductIDNumber = ?)

How do I assign the...say, ProductDescription to a variable called
prodDesc?

Thanks,
Tony



Cor,
查询不一定要说...一个文本框。我希望结果分配给变量

所以我可以在我的程序中的另一个地方使用它。

查询将始终只返回1行,因为ProductIDNumber在数据库中设置为

唯一。


我如何获取ProductDescription的值以分配给变量

strProdDesc或将ProductID分配给intProdID?


谢谢,

Tony


" Cor Ligthert [MVP]" < no ************ @ planet.nlwrote in message

news:eD ************* @ TK2MSFTNGP06.phx。 gbl ...
Cor,
The query is not bound to say... a text box. I want the result to be
assigned to a variable so I can use it in another place in my program. The
query will always return only 1 row because the ProductIDNumber is set to
unique in the database.

How do I get the value for ProductDescription to be assigned to the variable
strProdDesc or assign the ProductID to intProdID?

Thanks,
Tony

"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:eD*************@TK2MSFTNGP06.phx.gbl...

tony,


我不确定你的问题是什么,


你想要这个exression的第一行而不是添加Top1

如果你想创建这个表达式的一行而不是添加Distinct


如果你只想要一行返回而不是使用datareader,


Cor


" Tony K" < ki ******** @ berst的NOSPAMcomcast.netschreef

新闻:好的************** @ TK2MSFTNGP03.phx.gbl .. 。
tony,

I am not sure of what your question is,

Do you want the first row of this exression than add Top1
If you want to make one row of this expression than add Distinct

If you only want one row to return than use the datareader,

Cor
"Tony K" <ki********@NOSPAMcomcast.netschreef in bericht
news:Ok**************@TK2MSFTNGP03.phx.gbl...

>如何将SQL查询的结果分配给变量。以下语句的结果将总是导致返回1行。

SELECT ProductDescription,ProductID
FROM Products
WHERE(ProductIDNumber =?)

如何将...说明ProductDescription分配给名为
prodDesc的变量?

谢谢,
Tony
>How would I assign the result of a SQL query to a variable. The result
of the following statement will ALWAYS result in 1 row returned.

SELECT ProductDescription, ProductID
FROM Products
WHERE (ProductIDNumber = ?)

How do I assign the...say, ProductDescription to a variable called
prodDesc?

Thanks,
Tony



这篇关于分配给变量的SQL结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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