[SQL]批量修改存储过程视图

时间:2024-10-02 23:35:33

存储过程与视图适用

DECLARE @DBName VARCHAR(200);
DECLARE @ProcName VARCHAR(200)='w_sp_Sms_ExpeOrKeepEmpl';
DECLARE C_TABLES CURSOR FAST_FORWARD FOR
SELECT NAME
FROM MASTER..SYSDATABASES
WHERE NAME LIKE 'AB%' OR NAME LIKE 'DF%' OR NAME='ManChengZhuanYeSL'
ORDER BY NAME; OPEN C_TABLES;
FETCH NEXT FROM C_TABLES INTO @DBName;
WHILE @@FETCH_STATUS=0
BEGIN -- 不能使用OBJECTPROPERTY方法和sysobjects表判断存储过程,必须先USE数据库后才能查到
IF OBJECT_ID('[' + @DBName + '].[dbo].[' + @ProcName + ']') IS NOT NULL
BEGIN
PRINT @DBName; -- 打印成功操作数据库,错误可在信息查看(普通错误直接提示,致命错误直接终止)
EXEC('
USE [' + @DBName + '];
EXEC(''
-- 单引号1个换4个 -- 可替换成视图
ALTER PROC [dbo].[' + @ProcName + ']
@BillGuid char(36)
AS
SELECT
a.BillGuid,d.EmplPhone,
(''''单号''''+c.BillNo+'''' / ''''+c.MemberName+''''[''''+c.Customer+'''']'''') as FirstData,
(b.ItemName+''''[实收''''+CONVERT(varchar(100),b.ItemAmount)+''''元]'''') as Keyword1Data,
CONVERT(INT, b.ItemCount) as Keyword2Data,
c.CheckoutTime as Keyword3Data,
(a.EmplNo+''''_''''+a.EmplName+''''(''''+e.LevelName+'''')'''' + Char(13) + Char(10) +(case when a.AssignMark=1 then ''''指定'''' else ''''轮牌'''' end) +'''':业绩 ''''+CONVERT(varchar(100),a.Perfor)+'''' / 提成 ''''+CONVERT(varchar(100),(a.BasicRoya+a.AssignRoya))+'''' / 创单 ''''+CONVERT(varchar(100),a.InventRoya)
+ Char(13) + Char(10)+ ''''付款方式:'''' + ISNULL(STUFF((SELECT '''' / '''' + PaymentName + '''' '''' + CONVERT(VARCHAR, PayMoney) FROM View_MergeExpePayment WHERE BillGuid=@BillGuid FOR XML PATH('''''''')), 1, 3, ''''''''), ''''无'''')) as Keyword4Data,
'''''''' as RemarkData
FROM View_MergeExpeEmpl as a
INNER JOIN View_MergeExpeItem as b on a.ExpeItemGuid=b.ExpeItemGuid AND b.CheckoutMark=3 AND b.DeleteMark=0
INNER JOIN View_MergeExpeBill as c on a.BillGuid=c.BillGuid AND c.CheckoutMark=3 AND c.DeleteMark=0
INNER JOIN SalonEmplBasic as d on a.EmplGuid=d.EmplGuid
INNER JOIN SalonEmplLevel e on a.LevelGuid=e.LevelGuid AND a.StoresGuid=e.StoresGuid
WHERE a.BillGuid=@BillGuid AND a.CheckoutMark=3 AND a.DeleteMark=0
UNION ALL
SELECT a.BillGuid,d.EmplPhone,
(''''单号''''+b.BillNo+'''' / ''''+e.MemberName+''''[''''+b.VipNo+'''']'''') as FirstData,
(case when a.KeepType=1 then ''''开卡'''' else ''''充值'''' end)as Keyword1Data,
1 as Keyword2Data,
b.KeepTime as Keyword3Data,
(c.EmplNo+''''_''''+c.EmplName+Char(13) + Char(10)+''''业绩 ''''+CONVERT(varchar(100),a.PerforMoney)+'''' / 提成 ''''+CONVERT(varchar(100),a.RoyaMoney)
+Char(13) + Char(10)+''''付款方式:'''' + ISNULL(STUFF((SELECT '''' / '''' + PaymentName + '''':'''' + CONVERT(VARCHAR, PayMoney) FROM SalonKeepPayment WHERE BillGuid=@BillGuid FOR XML PATH('''''''')), 1, 3, ''''''''), ''''无'''')) as Keyword4Data,
'''''''' as RemarkData
FROM SalonKeepEmpl as a
INNER JOIN SalonKeepBill as b on a.BillGuid=b.BillGuid
INNER JOIN SalonEmpl as c on a.EmplGuid=c.EmplGuid
INNER JOIN SalonEmplBasic as d on c.EmplGuid=d.EmplGuid
INNER JOIN SalonMember as e on b.MemberGuid=e.MemberGuid
WHERE a.BillGuid=@BillGuid; '')
');
END;
FETCH NEXT FROM C_TABLES INTO @DBName;
END
CLOSE C_TABLES;
DEALLOCATE C_TABLES;

注:使用SP_MSFOREACHDB受2000个字符限制,不得不改用游标