数据库问题
我写了一个像下面这样的光标:
declare myCursor cursor
for select productID, productName from products
declare @productID int
declare @productName nvarchar(50)
open myCursor
fetch next from myCursor into @productID,@productName
print @productID
print @productName
set @productID=0
set @productName=''
while @@FETCH_STATUS=0
begin
fetch next from myCursor into @productID,@productName
print @productID
print @productName
set @productID=0
set @productName=''
end
close myCursor
deallocate myCursor
现在它在彼此下面打印产品的ID和名称,例如:
1
Coffee
2
Apple …
但我希望每个产品的ID和名称在同一行中,例如:
1 coffee
2 apple …
我能做什么?我将 id 转换为字符串,并使用 +''+ 将 id 和 name 连接在同一个字符串中。但由于 id 和名称的长度不同,因此没有干净的结果。还有其他方法可以做到这一点吗?
I have written a cursor like bellow :
declare myCursor cursor
for select productID, productName from products
declare @productID int
declare @productName nvarchar(50)
open myCursor
fetch next from myCursor into @productID,@productName
print @productID
print @productName
set @productID=0
set @productName=''
while @@FETCH_STATUS=0
begin
fetch next from myCursor into @productID,@productName
print @productID
print @productName
set @productID=0
set @productName=''
end
close myCursor
deallocate myCursor
now it print the id and name of the product under each other like:
1
Coffee
2
Apple …
But I want to have the id and name of the each product in a same line like:
1 coffee
2 apple …
What can I do? I converted the id into the String and use +’’+ to concat id and name in a same string. But as the ids and names don’t have same lengths, it didn’t have a clean result. Is there any other way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
尝试使用 TAB
或使用 NCHAR
try by using a TAB
or by using NCHAR
根据您的号码长度:
Char 会在号码右侧填充额外的空格,为您提供固定的号码。
Depending on how long your number can be:
Char will right-pad the number with extra spaces, giving you a fixed with for the number.
一开始,您可以确定最长数字的长度
,然后将其合并到您的打印语句中,就像
我在 SSMS 中使用“结果作为文本”而不是游标一样。希望这只是一个学习练习!
At the beginning you could determine the length of the longest number
Then incorporate it into your print statement like
I would just use "Results as text" in SSMS for this rather than a cursor. Hopefully it is just a learning exercise!
我想更简单的解决方案是在客户端应用程序中定义格式化规则,但如果您确实在数据库中需要它,这很简单,为什么要在解决方案中使用游标:
I guess simpler solution would be to define formatting rules in client application, but if you really need it in database this is simple, why to use cursor as in your solution:
您可以使用这样的表来代替使用游标......
Instead of using cursors you could use a table like this...
为什么你使用游标进行简单的提取..它非常慢并且一次只能处理一行!不惜一切代价远离光标!
您可以使用简单的 select 语句将它们作为新列检索。
第一部分选择产品 ID 作为字符串。然后它包含一个“空格”(“ ”),然后将产品名称连接到其末尾。
你最终会得到
1 Apple
2 Banana
等等,它会比你当前的光标快 1000 倍
希望有帮助,
Wes
Why are you using a cursor for a simple fetch.. it's incredibly slow and will only process one row at a time! Stay clear of cursors at ALL costs!
You could retrieve them both as a new column with a simple select statement.
The first part selects the Product ID as a string.. then it contaonates a 'space' (' ') then concatones the product name to the end of it.
You'd end up with
1 Apple
2 Banana
etc etc, and it'd be 1000x quicker than your current cursor
Hope that helps,
Wes