首页 > 解决方案 > SQL SP 中未附加 WHERE 子句

问题描述

我正在尝试运行如下的简单查询,以过滤出状态结果。如果活动、非活动或全部。

  IF (@Status = ''
    OR @Status IS NULL)
  BEGIN
    SET @Status = 'ALL'
  END

  DECLARE @strParam AS varchar(max)

  SET @strParam =  'Status: ' +
  REPLACE(@Status, '''', '')

  SET @strSql = 'SELECT [UserName],[FirstName],[LastName],[Email],[IsActive],''' + @strParam + '''  As Param
    ,''' + @strLink
  + ''' AS CompanyLogo
FROM [gblUserMaster] '

  print @strsql
  print @Status
  IF @Status = 'Active'
  BEGIN
    SET @strSql = @strSql + ' WHERE IsActive = 1'
      print @strsql
  END
  IF @Status = 'Inactive'
  BEGIN
    SET @strSql = @strSql + ' WHERE IsActive = 0'
      print @strsql
  END

  SET @strSql = @strSql + ' ORDER BY gblUserMaster.UserName'
  print @strsql
  EXEC (@strSql)

@Status = 活动我得到了结果,但是 WHERE 子句没有得到附加....打印给我下面的查询

SELECT [UserName],[FirstName],[LastName],[Email],[IsActive],'Status: Active'  As Param
    ,'' AS CompanyLogo
FROM [gblUserMaster]  ORDER BY gblUserMaster.UserName

但是没有 WHERE 子句我做错了什么?

标签: sql-servertsqlstored-proceduresdynamic-sql

解决方案


推荐阅读