動態(tài)SQL中返回數(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)文章
DBCC CHECKIDENT 重置數(shù)據(jù)庫標(biāo)識列從某一數(shù)值開始
DBCC CHECKIDENT 重置數(shù)據(jù)庫標(biāo)識列從某一數(shù)值開始2009-10-10使用BULK INSERT大批量導(dǎo)入數(shù)據(jù) SQLSERVER
使用BULK INSERT大批量導(dǎo)入數(shù)據(jù) SQLSERVER,需要的朋友可以參考下。2011-12-12mssql2005,2008導(dǎo)出數(shù)據(jù)字典實現(xiàn)方法
在項目開發(fā)過程中會用到數(shù)據(jù)字典,本文將詳細介紹mssql2005,2008如何導(dǎo)出數(shù)據(jù)字典,需要了解更多的朋友可以參考下2012-11-11淺析SQL Server的聚焦使用索引和查詢執(zhí)行計劃
本文通過介紹默認使用索引、強制使用聚集索引、強制使用非聚集索引讓我們知道對于檢索所有列結(jié)果集使用主鍵的聚集索引是最佳選擇。有興趣的朋友可以看下2016-12-12揭秘SQL Server 2014有哪些新特性(3)-可更新列存儲聚集索引
可更新的列存儲索引作為SQL Server 2014的一個關(guān)鍵功能之一,在提升數(shù)據(jù)庫的查詢性能方面貢獻非常突出。據(jù)微軟統(tǒng)計,在面向OLAP查詢統(tǒng)計類系統(tǒng)中,相比其他SQL傳統(tǒng)版本的數(shù)據(jù)庫,報表查詢的性能最大可提升上十倍。2014-08-08SQL 雙親節(jié)點查找所有子節(jié)點的實現(xiàn)方法
下面小編就為大家?guī)硪黄猄QL 雙親節(jié)點查找所有子節(jié)點的實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-05-05