如何把SQL中查询的字段赋给一个变量?

来源:百度知道 编辑:UC知道 时间:2024/04/30 06:47:05
比如northwind中Customers表里有个companyname字段,我用"select companyname from Customers where customerID = ALFKI" 查询出的结果是“Alfreds Futterkiste”, 我如何把“Alfreds Futterkiste”这个值赋给一个第一好的变量@str,哪位大虾告诉我这个T-sql语句怎么写啊?
我要的是sql server里的T-sql 代码 ,不是.NET或者JAVA代码

select @str=companyname from Customers where customerID = ALFKI"

String s;
ResultSet rs = stmt.executeQuery("select companyname from Customers where customerID = ALFKI"");
while(rs.hasNext()){
s = rs.next();
}
不知道你看得懂不,需要对jdbc的编程有一定的了解,这也是jdbc中查询的一般用法。第二句的意思是执行这条SQL语句,返回给一个结果集rs,第三句是循环遍历这个结果集,并赋值给字符串s

这个不太清楚了 ,你可以看看子查询,能不能实现你要的东西。