数据库问题

发布于 2024-09-15 17:53:58 字数 795 浏览 3 评论 0原文

我写了一个像下面这样的光标:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

戏剧牡丹亭 2024-09-22 17:53:58

尝试使用 TAB

print convert(nvarchar(30),@productID) + char(9) + @productName

或使用 NCHAR

print convert(nvarchar(8),@productID) +  @productName

try by using a TAB

print convert(nvarchar(30),@productID) + char(9) + @productName

or by using NCHAR

print convert(nvarchar(8),@productID) +  @productName
倾听心声的旋律 2024-09-22 17:53:58

根据您的号码长度:

print convert(char(10), @productID) + ' ' + @productName

Char 会在号码右侧填充额外的空格,为您提供固定的号码。

Depending on how long your number can be:

print convert(char(10), @productID) + ' ' + @productName

Char will right-pad the number with extra spaces, giving you a fixed with for the number.

澜川若宁 2024-09-22 17:53:58

一开始,您可以确定最长数字的长度

DECLARE @length INT

SELECT @length = CAST(LOG10(MAX(productID)) AS INT)+1 FROM products

,然后将其合并到您的打印语句中,就像

PRINT LEFT(CAST(@productID AS VARCHAR(10)) + 
    SPACE(@length),@length) + ' ' + @productName

我在 SSMS 中使用“结果作为文本”而不​​是游标一样。希望这只是一个学习练习!

At the beginning you could determine the length of the longest number

DECLARE @length INT

SELECT @length = CAST(LOG10(MAX(productID)) AS INT)+1 FROM products

Then incorporate it into your print statement like

PRINT LEFT(CAST(@productID AS VARCHAR(10)) + 
    SPACE(@length),@length) + ' ' + @productName

I would just use "Results as text" in SSMS for this rather than a cursor. Hopefully it is just a learning exercise!

我不是你的备胎 2024-09-22 17:53:58

我想更简单的解决方案是在客户端应用程序中定义格式化规则,但如果您确实在数据库中需要它,这很简单,为什么要在解决方案中使用游标:

SELECT left(convert(varchar(20), productID) + '      ',6) + ' - ' + productName
from products

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:

SELECT left(convert(varchar(20), productID) + '      ',6) + ' - ' + productName
from products
段念尘 2024-09-22 17:53:58

您可以使用这样的表来代替使用游标......

DECLARE @products TABLE (ProductID int, ProductName nvarchar(50), RowIndex int IDENTITY(1,1))

INSERT INTO @products (ProductID, ProductName) SELECT ProductID, ProductName FROM products

DECLARE @totalRows int
DECLARE @rowIndex int

SELECT 
    @totalRows = COUNT(RowIndex), 
    @rowIndex = 1 
FROM @products

DECLARE @ProductID int
DECLARE @ProductName nvarchar(50)

WHILE(@rowIndex < @totalRows)
BEGIN

    SELECT @ProductID = ProductID, @ProductName = ProductName FROM @products WHERE RowIndex = @rowIndex

    -- Do here your stuff...
    PRINT LEFT(CAST(@productID as varchar) + '      ',6) + ' - ' + @productName 

    SET @rowIndex = @rowIndex + 1   

END

Instead of using cursors you could use a table like this...

DECLARE @products TABLE (ProductID int, ProductName nvarchar(50), RowIndex int IDENTITY(1,1))

INSERT INTO @products (ProductID, ProductName) SELECT ProductID, ProductName FROM products

DECLARE @totalRows int
DECLARE @rowIndex int

SELECT 
    @totalRows = COUNT(RowIndex), 
    @rowIndex = 1 
FROM @products

DECLARE @ProductID int
DECLARE @ProductName nvarchar(50)

WHILE(@rowIndex < @totalRows)
BEGIN

    SELECT @ProductID = ProductID, @ProductName = ProductName FROM @products WHERE RowIndex = @rowIndex

    -- Do here your stuff...
    PRINT LEFT(CAST(@productID as varchar) + '      ',6) + ' - ' + @productName 

    SET @rowIndex = @rowIndex + 1   

END
神爱温柔 2024-09-22 17:53:58

为什么你使用游标进行简单的提取..它非常慢并且一次只能处理一行!不惜一切代价远离光标!

您可以使用简单的 select 语句将它们作为新列检索。

select convert(nvarchar(5),productID) + ' ' + productName as 'ID_Name' from products

第一部分选择产品 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.

select convert(nvarchar(5),productID) + ' ' + productName as 'ID_Name' from products

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文