动态SQL语法及获取本周、本月、本年时间段值

 

if   exists ( select   *   from  sysobjects  where  name = N ' Week_GetTask '   and  type = ' P ' )
drop   proc  Week_GetTask
go
CREATE   PROC  Week_GetTask
@timeRange   datetime ,
@dep_id   int ,
@type   varchar ( 10 ),
@finshable   varchar ( 10 )
AS
    
DECLARE   @timeStart   datetime , @timeEnd   datetime , @sql   varchar ( 500 )

     
IF ( @type = ' week ' )
    
-- 获取周时间段值  
         BEGIN     
            
SELECT   @timeStart   =   Dateadd (wk, datediff (wk, 0 , @timeRange ), - 1 )
            
SELECT   @timeEnd   =   Dateadd (wk, datediff (wk, 0 , @timeRange ), 6 )
    
END
     
ELSE  
    
IF ( @type = ' month ' )
        
-- 获取月时间段值
         BEGIN
            
SELECT   @timeStart   =   Dateadd (mm, datediff (mm, 0 , @timeRange ), 0 )
            
SELECT   @timeEnd   =   Dateadd (ms, - 3 , dateadd (mm, datediff (m, 0 , @timeRange ) + 1 , 0 ))
        
END
    
ELSE
        
-- 获取年时间段值
         BEGIN
            
SELECT   @timeStart   =   Dateadd (yy, datediff (yy, 0 , @timeRange ), 0 )
            
SELECT   @timeEnd   =   Dateadd (ms, - 3 , Dateadd (yy, datediff (yy, 0 , @timeRange ) + 1 , 0 ))
        
END

SET   @sql   =   ' select mes_id,title,multiPrincipalName '
SET   @sql   =   @sql + '  from [message]  '
SET   @sql   =   @sql + '  where dep_id= '
SET   @sql   =   @sql +   cast ( @dep_id   as   char ( 10 ))
SET   @sql   =   @sql + '  and  '
SET   @sql   =   @sql + '''' + convert ( varchar ( 10 ), @timeStart , 120 ) + ''''
SET   @sql   =   @sql + ' <=add_date and  '
SET   @sql   =   @sql + '''' + convert ( varchar ( 10 ), @timeEnd , 120 ) + ''''
SET   @sql   =   @sql + ' >= add_date '
SET   @sql   =   @sql + '  and towho<> ''''  and multiPrincipalName<> ''''   '

IF ( @finshable = ' yes ' )
BEGIN
    
SET   @sql   =   @sql   +   '  and finishRate=100 '
END
ELSE
BEGIN
    
SET   @sql   =   @sql   +   '  and finishRate<>100 '
END

PRINT   @sql      
Exec ( @sql

GO



动态SQL基本语法文章,以下内容是转载
地址:http://tmsoft.lsxy.com/index.php?load=read&id=74

1 :普通SQL语句可以用Exec执行

Select   *   from   tableName 
 
Exec ( ' select * from tableName '
 
Exec  sp_executesql  N ' select * from tableName '

-- 请注意字符串前一定要加N


2:字段名,表名,数据库名之类作为变量时,必须用动态SQL 
declare   @fname   varchar ( 20
 
set   @fname   =   ' FiledName '  
 
Select   @fname   from  tableName

  -- 错误,不会提示错误,但结果为固定值FiledName,并非所要。
Exec ( ' select  '   +   @fname   +   '  from tableName ' )
  -- 请注意 加号前后的 单引号的边上加空格

 

当然将字符串改成变量的形式也可

declare   @fname   varchar ( 20
 
set   @fname   =   ' FiledName '   -- 设置字段名 
  
declare   @s   varchar ( 1000
 
set   @s   =   ' select  '   +   @fname   +   '  from tableName '  
 
Exec ( @s )                 --  成功 
  exec  sp_executesql  @s     --  此句会报错</p> 
< p > declare   @s   Nvarchar ( 1000 )   --  注意此处改为nvarchar(1000) 
  set   @s   =   ' select  '   +   @fname   +   '  from tableName '  
 
Exec ( @s )                 --  成功    
  exec  sp_executesql  @s     --  此句正确


3. 输出参数

declare   @num   int
         
@sqls   nvarchar ( 4000
 
set   @sqls = ' select count(*) from tableName '  
 
exec ( @sqls
-- 如何将exec执行结果放入变量中? 
  
declare   @num   int
                
@sqls   nvarchar ( 4000
 
set   @sqls = ' select @a=count(*) from tableName  '  
 
exec  sp_executesql  @sqls ,N ' @a int output ' , @num  output 
 
select   @num

你可能感兴趣的:(动态sql)