1
我已經聲明並設置了一些變量。然後我想使用該變量值作爲執行存儲過程的參數。但我總是得到錯誤如何在執行存儲過程中使用變量參數?
必須聲明標量變量。
這裏是我的變量:
--declare variables for parameter @listweek
declare @tgl_other varchar(50);
--set variables for parameter @listweekset
set @tgl_other = (select top 1 ltrim(rtrim(cast(numweek as char)))+'-('+rangeweek+')' from tbl_weeklyflash where year(dates) = year(getdate()) order by numweek desc);
--the variable values should be like this 30-(01-03-2012 - 08-03-2012)
--and this value that I want to use as parameter value in executing stored procedure
,這是執行存儲過程:
insert into my_table (field1, field2, field3)
EXEC my_store_procedure @tgl_other
-- the parameter for my_store_procedure is like '30-(01-03-2012 - 08-03-2012)'
這是我的變量不正確?或者我錯誤的方式來使用變量的參數?
UPDATE:
如果我執行這個查詢:
declare @tgl_other varchar(50);
set @tgl_other = (select top 1 ltrim(rtrim(cast(numweek as char)))+'-('+rangeweek+')' from tbl_weeklyflash where year(dates) = year(getdate()) order by numweek desc);
EXEC my_store_procedure @tgl_other
它工作正常,但如果我添加INSERT INTO..
聲明它不會工作。爲什麼會發生?
UPDATE II: 這裏是整個查詢,我嘗試運行
declare @tgl_other varchar(50);
set @tgl_other = (select top 1 ltrim(rtrim(cast(numweek as char)))+'-('+rangeweek+')' from tbl_weeklyflash where year(dates) = year(getdate()) order by numweek desc);
TRUNCATE TABLE mytable
GO
INSERT INTO mytable (field1, field2)
EXEC my_store_procedure @tgl_other
GO
sql似乎是正確的。你執行整個sql批處理還是隻是插入到...語句? – praveen 2012-07-31 04:25:56
@praveen我執行整個sql批處理。我已更新我的問題以獲取更多詳細信息 – blankon91 2012-07-31 04:36:55
您可以發佈您的SP嗎? – praveen 2012-07-31 04:42:03