2010-08-24 103 views
1

我写了一个光标像波纹管:数据库问题

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 

现在打印产品的标识和名称下对方喜欢:

1 
Coffee 
2 
Apple … 

但我想有id和每个产品的名称在同一行中,例如:

1 coffee 
2 apple … 

我该怎么办?我将id转换为String并使用+''+将id和name连接到同一个字符串中。但由于ID和名称长度不一样,所以没有一个清晰的结果。有没有其他方法可以做到这一点?

+2

你为什么要用这个游标? – codingbadger 2010-08-24 11:30:16

+0

最常见的答案可能是这应该是客户端应用程序的责任,但是,您需要将其格式化到什么位置?在管理工作室,客户端应用程序,其他地方? – 2010-08-24 11:31:06

+0

@Barry - 我是HOPING不止一个产品名称可以有产品ID,他们也不想重复产品ID。我希望。 – LittleBobbyTables 2010-08-24 11:32:04

回答

2

尝试使用TAB

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

或使用NCHAR

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

这不是我的电脑现在我没有这个服务器上的sql服务器,但我会稍后尝试。有没有另一种方式? – sara 2010-08-24 11:32:52

1

根据您的号码能维持多久是:

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

字符将右垫数量有额外的空间,给你一个固定的数字。

+0

谢谢,我会试试 – sara 2010-08-24 11:38:37

0

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

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

为什么使用一个varchar当你刚刚用空格填充它?为什么只有一个整数可以达到10时的左边六位数? – LittleBobbyTables 2010-08-24 11:38:34

1

在开始的时候你可以确定最长号码

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中使用“结果作为文本”而不​​是光标。希望这只是一个学习练习!

+0

我认为这是最好的解决方案,谢谢 – sara 2010-08-24 11:43:29

0

而不是使用游标,你可以使用这样的表...

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 
+0

我需要光标,但我也会尝试你的代码,谢谢你的帮助 – sara 2010-08-24 11:42:16

+0

你需要什么光标?我可以想象一个真正需要的用例...... – 2010-08-24 12:51:34

0

你为什么要使用的游标简单读取..这是令人难以置信的慢,只能一次处理一个行!不惜一切代价保持光标不变!

你可以通过一个简单的select语句将它们作为一个新列进行检索。

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

第一部分选择产品ID作为字符串..然后它contaonates一个“空间”(”“)然后concatones产品名称以它的结束。

你想最终

1苹果

2香蕉

等等等等,它会比你当前光标

希望帮助更快的1000倍,

Wes