我想使用局部變量即。數據類型INT的@CV_Max_Len作爲參數在打印時用作CHAR。以下是我的代碼。使用int數據類型的局部變量作爲char()中的參數
declare cd_2 cursor for
select compact_disc_id, cd_title, price, category
from compact_disc_inventory for read only
declare @CV_Id int
declare @CV_Title varchar(60), @CV_Category varchar(60)
declare @CV_Price numeric(5,2)
open cd_2
declare @CV_Max_Len int
set @CV_Max_Len = (select max(len(cd_title)) from compact_disc_inventory)
print @CV_Max_Len
fetch next from cd_2 into @CV_Id, @CV_Title, @CV_Price, @CV_Category
-- print 'Id Title Price Category'
while @@fetch_status = 0
begin
print convert(varchar(6),@CV_Id) + space(10) + convert(char(@CV_Max_Len),@CV_Title) +
space(5) + convert(varchar(6),@CV_Price) + space(5) + @CV_Category
fetch next from cd_2 into @CV_Id, @CV_Title, @CV_Price, @CV_Category
end
close cd_2
deallocate cd_2
,但我得到以下錯誤
Msg 102, Level 15, State 1, Line 19
Incorrect syntax near '@CV_Max_Len'.
任何幫助嗎?
@ocasoprotal,如何以正確的格式在第一個地方只鍵入它?你能指導我一點嗎?謝謝 – Lalu
只需標記您的sourceode,並在輸入字段上方用兩個大括號{}來按圖標。之後您也可以隨時使用標題爲_edit_的linke編輯帖子。順便說一句:我發現你的源代碼很難閱讀所有混合在聲明語句。 –
你想要完成什麼?忘記實施。它看起來像你想要填充一個字符串。你想要左或右墊?你想如何處理截斷?有辦法做到這一切; CHAR數據類型不是必需的(也不是你想要做甚至可能的)。 –