スペスexecutesql使用テスト記録

1684 ワード


/*execute          ,   exec,          ,         Sql 
 sp_executesql,sql2005            ,        sql ,  : 
exec sp_executesql @sql, N'@count int out,@id varchar(20)', @cou out 
 ,@id 
 @sql      sql 
 N'@count int out,@id varchar(20)'      sql       
@cou out,@id    sql                
*/--            ?
-- 1.                ,        
 declare @sqlstr nvarchar(2000) 
 declare @DJBHstr varchar(20) 
 set @DJBHstr='DA6160405019970' 
 set @sqlstr='select @count=sum(sl) from QDTHDmx where DJBH=' +@DJBHstr
 exec @sqlstr 
 /**
    2812,   16,   62,  8  
        'select @count=sum(sl) from QDTHDmx where DJBH=DA6160405019970'。
 */
--2.      count(*)   ,    exec      ,   sp_executesql        :
 
 declare @sql nvarchar(2000) 
 declare @cou int 
 declare @DJBH varchar(20) 
 set @DJBH='DA6160405019970' 
 set @sql='select @count=sum(sl) from QDTHDmx where DJBH=@DJBH' 
 exec sp_executesql @sql, N'@count int out,@DJBH varchar(20)', @cou out 
 ,@DJBH 
 print @cou 

 /*
 2.   
     ,   exec,       @id   ,       @sql    ,       Sql2005            Sql       
   sp_executesql    ,        ,      Sql      ,            ,               ,        ! 

   : 
 1.sp_executesql    Sql   Sql       Nvarchar,       @sql,N'@count int out,@id varchar(20)'    sql2005 Varchar    ,     Sp3       ,   Nvarchar 
2.  Sql                      , : 
N'@count int out,@id varchar(20)', @cou out,@id 
 @count    @cou,@id  @id 
     ,      , : 
N'@count int out,@id varchar(20)', @id=@id, @count=@cou out 
3.  SQl                        
 */