如何在 SQL 服务器的存储过程中执行字符串
How to execute a string in a stored procedure in SQL Server
我创建了一个存储过程并调用它:
ALTER PROCEDURE [dbo].[SP_Maketing]
-- Add the parameters for the stored procedure here
@Action nvarchar(50) = null,
@Brank nvarchar(10) = null,
@DateImport nvarchar(30) = null,
@Like nvarchar(30) = null,
@Month int = 0,
@Year int = 0,
@DateUpDate nvarchar(30) = null,
@SchemeCode nvarchar(30) = null,
@Teritory nvarchar(50) = null,
@FromDate nvarchar(30) = null,
@ToDate nvarchar(30) = null,
@CouponCode nvarchar(10) = null,
@BarCode nvarchar(20) = null,
@FromPoint int = 0,
@ToPoint int = 0
AS
BEGIN
If(@Action = 'GETCUSTOMEROFSTORE')
Begin
If(@Brank = 'VASCARA')
Begin
Set @Like = 'VAS%'
Set @DateImport = '01/01/2015'
End
If(@Brank='CATWALK')
Begin
Set @Like = 'CAT%'
Set @DateImport = '05/01/2015'
End
Set @StrSQL = 'Select MSC.[Card No_], MSC.[Account No_], MC.Name, MC.[First Name],
MC.[Middle Name], MC.Surname, MSC.[Club Code], MSC.[Scheme Code], ISNULL(TH.[Sale Point], 0) As ''Sale Point'',
ISNULL(TH1.[Sale Point], 0) As ''Sale Point Of Year'',
MC.[Date of Birth], MC.[Address], MC.[Address 2], MC.[Teritory], MC.Country_VAS, MC.City, MC.[Phone No_],
MC.[Mobile Phone No_], MSC.[Allocated to Store],
MC.[E-Mail], UPS.[Last Scheme], UPS.[Current Scheme], UPS.Date
FROM [MASTER$Membership Card] MSC
left join (
select A.[Account No_], A.[Name], A.[First Name], A.[Middle Name], A.Surname, A.[Date of Birth],
A.[Address], A.[Address 2], B.[Name] AS ''Teritory'', A.Country_VAS, A.City, A.[Phone No_], A.[Mobile Phone No_],A.[E-Mail]
from [MASTER$Member Contact] A
left join (
select * from [MASTER$Territory]
) B on B.[Code]= A.[Territory Code]
) MC on MC.[Account No_]=MSC.[Account No_]
left join (
Select Sum([Points]) as ''Sale Point'', [Account No_] From [MASTER$Member Point Entry]
Where [Member Scheme] like '''+@Like+'''
Group By [Account No_]
)TH on TH.[Account No_] =MSC.[Account No_]
left join (
select Sum([Points]) as ''Sale Point'', [Account No_] from [MASTER$Member Point Entry]
where [Entry Type]=0 and [Member Scheme] like '''+@Like+'''
group by [Account No_]
)TH1 on TH1.[Account No_] =MSC.[Account No_]
Left Join (
select Distinct * from [MASTER$Member Account Upgrade Entry]
where [Action Type]=0 and ([Active]=1)
and [Date]>=CONVERT(date,'''+@DateImport+''',103)
and [Current Scheme] like '''+@Like+'''
) UPS on UPS.[Account No_]=MSC.[Account No_]
Where MSC.[Club Code] like '''+@Like+''' and MSC.[Account No_] in(
Select [Account No_] from [MASTER$Membership Card]
Where '+@Teritory+'
and [Club Code] like '''+@Like+'''
)'
EXEC(@StrSQL)
End
End
当我用查询调用它时:
exec SP_Maketing @Action = N'GETCUSTOMEROFSTORE',
@Brank = 'VASCARA',
@Teritory = N' [Allocated to Store] = ''VNB01'''
没关系!
但是,当我用查询调用它时:
exec SP_Maketing @Action = N'GETCUSTOMEROFSTORE',
@Brank = 'VASCARA',
@Teritory = N'[Allocated to Store] = ''VNB01'' or [Allocated to Store] = ''VNB02'''
显示错误:
Msg 207, Level 16, State 1, Line 551
Invalid column name 'Allocated to and [Club Code'.
您将参数长度 @Teritory nvarchar(50)
声明为 50 并传递长度超过 50.This 的值会导致截断,因此错误 message.Increase 相应的大小可能为 100在这种情况下。
AND
优先于 OR
。所以当你使用 both.After 增加尺寸时最好使用括号,试试这个
exec SP_Maketing @Action = N'GETCUSTOMEROFSTORE',
@Brank = 'VASCARA',
@Teritory = N'([Allocated to Store] = ''VNB01'' or [Allocated to Store] = ''VNB02'')'
我创建了一个存储过程并调用它:
ALTER PROCEDURE [dbo].[SP_Maketing]
-- Add the parameters for the stored procedure here
@Action nvarchar(50) = null,
@Brank nvarchar(10) = null,
@DateImport nvarchar(30) = null,
@Like nvarchar(30) = null,
@Month int = 0,
@Year int = 0,
@DateUpDate nvarchar(30) = null,
@SchemeCode nvarchar(30) = null,
@Teritory nvarchar(50) = null,
@FromDate nvarchar(30) = null,
@ToDate nvarchar(30) = null,
@CouponCode nvarchar(10) = null,
@BarCode nvarchar(20) = null,
@FromPoint int = 0,
@ToPoint int = 0
AS
BEGIN
If(@Action = 'GETCUSTOMEROFSTORE')
Begin
If(@Brank = 'VASCARA')
Begin
Set @Like = 'VAS%'
Set @DateImport = '01/01/2015'
End
If(@Brank='CATWALK')
Begin
Set @Like = 'CAT%'
Set @DateImport = '05/01/2015'
End
Set @StrSQL = 'Select MSC.[Card No_], MSC.[Account No_], MC.Name, MC.[First Name],
MC.[Middle Name], MC.Surname, MSC.[Club Code], MSC.[Scheme Code], ISNULL(TH.[Sale Point], 0) As ''Sale Point'',
ISNULL(TH1.[Sale Point], 0) As ''Sale Point Of Year'',
MC.[Date of Birth], MC.[Address], MC.[Address 2], MC.[Teritory], MC.Country_VAS, MC.City, MC.[Phone No_],
MC.[Mobile Phone No_], MSC.[Allocated to Store],
MC.[E-Mail], UPS.[Last Scheme], UPS.[Current Scheme], UPS.Date
FROM [MASTER$Membership Card] MSC
left join (
select A.[Account No_], A.[Name], A.[First Name], A.[Middle Name], A.Surname, A.[Date of Birth],
A.[Address], A.[Address 2], B.[Name] AS ''Teritory'', A.Country_VAS, A.City, A.[Phone No_], A.[Mobile Phone No_],A.[E-Mail]
from [MASTER$Member Contact] A
left join (
select * from [MASTER$Territory]
) B on B.[Code]= A.[Territory Code]
) MC on MC.[Account No_]=MSC.[Account No_]
left join (
Select Sum([Points]) as ''Sale Point'', [Account No_] From [MASTER$Member Point Entry]
Where [Member Scheme] like '''+@Like+'''
Group By [Account No_]
)TH on TH.[Account No_] =MSC.[Account No_]
left join (
select Sum([Points]) as ''Sale Point'', [Account No_] from [MASTER$Member Point Entry]
where [Entry Type]=0 and [Member Scheme] like '''+@Like+'''
group by [Account No_]
)TH1 on TH1.[Account No_] =MSC.[Account No_]
Left Join (
select Distinct * from [MASTER$Member Account Upgrade Entry]
where [Action Type]=0 and ([Active]=1)
and [Date]>=CONVERT(date,'''+@DateImport+''',103)
and [Current Scheme] like '''+@Like+'''
) UPS on UPS.[Account No_]=MSC.[Account No_]
Where MSC.[Club Code] like '''+@Like+''' and MSC.[Account No_] in(
Select [Account No_] from [MASTER$Membership Card]
Where '+@Teritory+'
and [Club Code] like '''+@Like+'''
)'
EXEC(@StrSQL)
End
End
当我用查询调用它时:
exec SP_Maketing @Action = N'GETCUSTOMEROFSTORE',
@Brank = 'VASCARA',
@Teritory = N' [Allocated to Store] = ''VNB01'''
没关系!
但是,当我用查询调用它时:
exec SP_Maketing @Action = N'GETCUSTOMEROFSTORE',
@Brank = 'VASCARA',
@Teritory = N'[Allocated to Store] = ''VNB01'' or [Allocated to Store] = ''VNB02'''
显示错误:
Msg 207, Level 16, State 1, Line 551
Invalid column name 'Allocated to and [Club Code'.
您将参数长度 @Teritory nvarchar(50)
声明为 50 并传递长度超过 50.This 的值会导致截断,因此错误 message.Increase 相应的大小可能为 100在这种情况下。
AND
优先于 OR
。所以当你使用 both.After 增加尺寸时最好使用括号,试试这个
exec SP_Maketing @Action = N'GETCUSTOMEROFSTORE',
@Brank = 'VASCARA',
@Teritory = N'([Allocated to Store] = ''VNB01'' or [Allocated to Store] = ''VNB02'')'