0
你可以幫我在這裏,我試圖獲得執行SQL語句作爲一個語句,而不使用字符串連接。代碼如下。然後我試着做這附近有一個錯誤,並提前如何使用sql server轉換沒有字符串連接的程序?
感謝,
ALTER PROCEDURE [dbo].[GetAdminData]
(
@FirstName varchar(50)= null,
@LastName varchar(50)= null,
@Email varchar(50)= null,
@StartDate datetime = null,
@EndDate datetime= null,
@RbValue bit = 0,
@ShareholderStatus int = 0,
@EmailStatus int = 0,
@State nvarchar(10) = 0,
@debug bit = 0
)
AS
DECLARE @sql nvarchar(MAX),
@paramlist nvarchar(4000)
IF @FirstName IS NOT NULL OR @LastName IS NOT NULL OR @Email IS NOT NULL OR (@StartDate IS NOT NULL And @EndDate IS NOT NULL)
OR @ShareholderStatus >= 0 OR @EmailStatus >= 0 OR @State IS NOT NULL
SELECT @sql = 'select TOP 1000 id, firstname, lastname, email, state, country, status,Emaildate1,
case when shareexchangestatus = 0 Then ''None''
when shareexchangestatus = 1 Then ''Saved''
when shareexchangestatus = 2 Then ''Submitted''
when shareexchangestatus = 3 Then ''Pending''
when shareexchangestatus = 4 Then ''ReSubmitted''
when shareexchangestatus = 5 Then ''Accepted''
when shareexchangestatus = 6 Then ''Paid''
when shareexchangestatus = 7 Then ''Transfered''
Else '''' END As shareexchangestatus,lastlogindate FROM dbo.zookeepers_ShareholderExchange z with (nolock) where 1=1 '
SELECT @sql = @sql + 'AND (@FirstName IS NULL OR z.firstname LIKE @FirstName + ''%'') '
SELECT @sql = @sql + 'AND (@LastName IS NULL OR z.lastname LIKE @LastName + ''%'') '
SELECT @sql = @sql + 'AND (@Email IS NULL OR z.email = @Email) '
IF @StartDate IS NOT NULL AND @EndDate IS NOT NULL
BEGIN
if @RbValue = 1
BEGIN
SELECT @sql = @sql + ' AND z.Emaildate1 BETWEEN convert(char(23), @StartDate, 126) AND convert(char(23), @EndDate, 126)'
END
else
BEGIN
SELECT @sql = @sql + ' AND z.lastlogindate BETWEEN convert(char(23), @StartDate, 126) AND convert(char(23), @EndDate, 126)'
END
END
IF @ShareholderStatus > 0
SELECT @sql = @sql + ' AND z.shareexchangestatus = @ShareholderStatus '
SELECT @sql = @sql + 'AND (@EmailStatus IS NULL OR z.emailstatus = @EmailStatus) '
SELECT @sql = @sql + 'AND (@State IS NULL OR z.state = @State)'
SELECT @sql = @sql + ' ORDER BY z.lastlogindate'
IF @debug = 1
PRINT @sql
SELECT @paramlist = '@FirstName nvarchar(50),
@LastName nvarchar(50),
@Email nvarchar(100),
@StartDate datetime,
@EndDate datetime,
@RbValue bit,
@ShareholderStatus int,
@EmailStatus int,
@State nvarchar(10)'
EXEC sp_executesql @sql, @paramlist,
@FirstName , @LastName, @Email, @StartDate,
@EndDate, @RbValue,@ShareholderStatus,@EmailStatus,
@State