2010-08-24 144 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