動態(tài)SQL中返回數(shù)值的實(shí)現(xiàn)代碼
更新時間:2011年12月20日 12:27:20 作者:
最近在做一個paypal抓取數(shù)據(jù)的程序,由于所有字段和paypal之間存在對應(yīng)映射的關(guān)系,所以所有的sql語句必須得拼接傳到存儲過程里去執(zhí)行
復(fù)制代碼 代碼如下:
ALTER proc [dbo].[sp_common_paypal_AddInfo]
(
@paypalsql varchar(max),--不包含用戶表的paypalsql語句
@paypalusersql varchar(max),--paypal用戶表的sql語句
@ebaysql varchar(max),--不包含用戶表的ebaysql語句
@ebayusersql varchar(max),--ebay的用戶表sql語句
@paypaluserwhere varchar(max),--paypal用戶表查詢ID語句
@ebayuserwhere varchar(max),--ebay用戶表查詢ID語句
@websql varchar(max),--web除去用戶表的sql語句
@webusersql varchar(max),--web用戶表的sql語句
@webwhere varchar(max),--web用戶表where之后的sql語句
@ebaystockflag varchar(10),--ebay訂單號生成規(guī)則
@webstockflag varchar(10)--web訂單號生成規(guī)則
)
as
set xact_abort on
begin transaction mytrans
begin try
declare @uid int--根據(jù)語句查找用戶ID
declare @execsql varchar(max)
declare @ebayuid int--根據(jù)語句查找用戶ID
declare @execebaysql nvarchar(max)--用sp_executesql 字段類型必須是nvarchar
declare @sql nvarchar(max)--用sp_executesql 字段類型必須是nvarchar
set @sql='select @a=ID from tb_TransactionCustomer where '+ convert(varchar(8000),@paypaluserwhere)
exec sp_executesql @sql,N'@a int output',@uid output
set @uid =ISNULL(@uid,0)--如果不這樣判斷 獲取的值可能為null用len()獲取不到長度
--存在paypal用戶id
if(@uid>0)
begin
set @execsql=@paypalsql-- 存在用戶信息
set @execsql= REPLACE(@execsql,'@uid',''+convert(varchar,@uid)+'')
end
else
begin
set @execsql=@paypalusersql+@paypalsql --不存在用戶信息
end
if(LEN(@websql)>0)--執(zhí)行web語句
begin
exec sp_common_WebSiteorder_AddInfo @websql, @webusersql, @webwhere ,@webstockflag
end
if(LEN(@ebaysql)>0)--執(zhí)行ebay語句
begin
--exec sp_common_Ebay_AddInfo @ebaysql, @ebayusersql, @ebayuserwhere ,@ebaystockflag
SELECT * FROM tb_EbayOrder WITH (TABLOCKX)
SELECT * FROM tb_EbayOrderList WITH (TABLOCKX)
SELECT * FROM tb_EbayOrderUserInfo WITH (TABLOCKX)
set @sql='select @b=ID from tb_EbayOrderUserInfo where '+ convert(varchar(8000),@ebayuserwhere)
exec sp_executesql @sql,N'@b int output',@ebayuid output
set @ebayuid =ISNULL(@ebayuid,0)
if(@ebayuid>0)
begin
set @execebaysql=@ebaysql--存在ebayuid
set @execebaysql= REPLACE(@execebaysql,'@ebayuid',''+convert(varchar,@ebayuid)+'')--必須替換 否則會報錯誤說必須聲明標(biāo)量變量
end
else
begin
set @execebaysql=@ebayusersql+@ebaysql --不存在ebayuid
end
set @execebaysql= REPLACE(@execebaysql,'@00',dbo.GetOrderNum(@ebaystockflag))--調(diào)用函數(shù)替換訂單編號
exec (@execebaysql)
end
exec(@execsql)
end try
begin catch
if(@@TRANCOUNT>0)
rollback transaction mytrans
end catch
if(@@TRANCOUNT>0)
begin
commit transaction mytrans
end
else begin
rollback transaction mytrans
end
相關(guān)文章
sqlserver下將數(shù)據(jù)庫記錄的列記錄轉(zhuǎn)換成行記錄的方法
sqlserver下將數(shù)據(jù)庫記錄的列記錄轉(zhuǎn)換成行記錄的方法分享,需要的朋友可以參考下。2011-07-07
Sqlserver創(chuàng)建用戶并授權(quán)的實(shí)現(xiàn)步驟
這篇文章主要介紹了Sqlserver創(chuàng)建用戶并授權(quán)的實(shí)現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
SQL Server 2005/2008 用戶數(shù)據(jù)庫文件默認(rèn)路徑和默認(rèn)備份路徑修改方法
本環(huán)境是SQL Server 2005 Standard Version 64-bit 和 SQL Server 2008 Standard Version 64-bit 雙實(shí)例同時安裝在一個2010-04-04
Windows Server 2008 Standard Version 64-bit OS上

