首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

储存过程如何让一条带有变量的SQL语句在变量前后加单引号

2012-03-21 
储存过程怎么让一条带有变量的SQL语句在变量前后加单引号ALTER PROCEDURE [dbo].[proc_business] @user va

储存过程怎么让一条带有变量的SQL语句在变量前后加单引号
ALTER PROCEDURE [dbo].[proc_business]
 @user varchar(20),
 @city varchar(20),
AS

BEGIN
 declare @wheretrue varchar(100)
 if(@city='') set @wheretrue='select DEPT_CODE from T_USERDEPT where USER_NO='+@user+''
 else set @wheretrue ='select DEPT_CODE from T_DEPT where BRANCH_CODE='+@city+''

 select * from P_BUSINESS_LIST where DEPT_CODE in (@wheretrue)
END

我是想根据@city这个参数有没有值来产生不同的条件,条件是产生了
select DEPT_CODE from T_DEPT where BRANCH_CODE=HGZL002
可是BRANCH_CODE,USER_NO这两个字段是varchar类型的,所以产生出来的SQL因为少了两个单引号怎么也查不到值。

[解决办法]

SQL code
set @wheretrue ='select DEPT_CODE from T_DEPT where BRANCH_CODE='''+@city+''''
[解决办法]
SQL code
if(@city='') set @wheretrue='select DEPT_CODE from T_USERDEPT where USER_NO='''+@user+'''' else set @wheretrue ='select DEPT_CODE from T_DEPT where BRANCH_CODE='''+@city+''''
[解决办法]
SQL code
ALTER PROCEDURE [dbo].[proc_business] @user varchar(20), @city varchar(20),ASBEGIN declare @wheretrue varchar(100) if(@city='') set @wheretrue='select DEPT_CODE from T_USERDEPT where USER_NO='''+ltrim(@user)+'''' else set @wheretrue ='select DEPT_CODE from T_DEPT where BRANCH_CODE='''+@city+'''' select * from P_BUSINESS_LIST where DEPT_CODE in (' +ltrim(@wheretrue)+ ')END
[解决办法]
SQL code
ALTER PROCEDURE [dbo].[proc_business] @user varchar(20), @city varchar(20),ASBEGIN declare @wheretrue varchar(100) if(@city='') set @wheretrue='select DEPT_CODE from T_USERDEPT where USER_NO='''+ltrim(@user)+'''' else set @wheretrue ='select DEPT_CODE from T_DEPT where BRANCH_CODE='''+@city+'''' exec('select * from P_BUSINESS_LIST where DEPT_CODE in (' +ltrim(@wheretrue)+ ')')END
[解决办法]
SQL code
ALTER PROCEDURE [dbo].[proc_business](@user varchar(20),@city varchar(20))ASBEGINdeclare @wheretrue varchar(100)if(@city='') set @wheretrue='select DEPT_CODE from T_USERDEPT where USER_NO='''+ltrim(@user)+''''elseset @wheretrue ='select DEPT_CODE from T_DEPT where BRANCH_CODE='''+@city+''''exec('select * from P_BUSINESS_LIST where DEPT_CODE in (' +@wheretrue+ ')')END
[解决办法]
双引号外面套单引号 '"+xxxxx+"'

热点排行