日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
sql server中動(dòng)態(tài)sql語句的應(yīng)用

sql server中應(yīng)該如何使用動(dòng)態(tài)sql語句呢?下面就為您詳細(xì)介紹sql server中動(dòng)態(tài)sql語句的應(yīng)用,希望可以讓您對(duì)動(dòng)態(tài)sql語句有更多的了解。

 
 
 
  1. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[insertMdfalarmInfo]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)  
  2. drop procedure [dbo].[insertMdfalarmInfo]  
  3. GO  
  4. SET QUOTED_IDENTIFIER OFF   
  5. GO  
  6. SET ANSI_NULLS OFF   
  7. GO  
  8. CREATE PROCEDURE insertMdfalarmInfo  
  9. @alarmID      int,            -- 告警器ID  
  10. @monitorEquID     varchar(16)  
  11.  
  12. AS  
  13. begin  
  14.    --drop table #table_tmp  
  15.    set @alarmID = 38 
  16.    create table #table_tmp   
  17.    (  
  18.    [id] int  
  19.    )  
  20.    set @monitorEquID = 6 
  21.     
  22.    declare @selectContainerIDsql NVARCHAR(130)  
  23.    set @selectContainerIDsql= 'select monitorSourceID from v_mdfAlarmPortInfo where monitorEquPort in (1,3,5) and monitorEquID = 6 group by monitorSourceID' 
  24.     
  25.     
  26.    insert into #table_tmp ([id]) EXECUTE sp_executesql @selectContainerIDsql  
  27.    declare countMonitorSourceID cursor for select id from #table_tmp  
  28.     
  29.    open countMonitorSourceID  
  30.    declare @monitorSourceID int  
  31.    fetch next from countMonitorSourceID into @monitorSourceID  
  32.    while @@fetch_status = 0 
  33.     begin  
  34.      print @monitorSourceID  
  35.      fetch next from countMonitorSourceID into @monitorSourceID  
  36.     end  
  37.    close countMonitorSourceID  
  38.    drop table #table_tmp  
  39.    deallocate countMonitorSourceID  
  40.  
  41. end   
  42. GO  
  43. SET QUOTED_IDENTIFIER OFF   
  44. GO  
  45. SET ANSI_NULLS ON   
  46. GO  
  47.  

相信大家都比較了解select * from tablename where aa=bb的用法和exec('select * from tablename where aa=bb')的用法 ,但是仍然有很多人不知道sp_executesql的用法,它可以讓動(dòng)態(tài)sql接收參數(shù)且把查詢結(jié)果返回到一個(gè)參數(shù)

--接收條件值參數(shù)的靜態(tài)sql

 
 
 
  1. declare @name varchar(100)   
  2. set @name='sysobjects'   
  3. select name from sysobjects where object_name(id)=@name   
  4. go  

--接收整個(gè)條件描述的簡(jiǎn)單動(dòng)態(tài)sql

 
 
 
  1. declare @where varchar(100)   
  2. set @where='object_name(id)=''sysobjects'''   
  3. exec('select name from sysobjects where '+@where)   
  4. go  

--接收整個(gè)條件描述,且把查詢返回到變量參數(shù)的復(fù)雜動(dòng)態(tài)sql

 
 
 
  1. declare @where nvarchar(100)   
  2. set @where=N'object_name(id)=''sysobjects'''   
  3. declare @ret varchar(100)   
  4. declare @sql nvarchar(1000)   
  5. set @sql=N'select @ret=name from sysobjects where '+ @where   
  6. exec    sp_executesql @sql,N'@ret varchar(100) output' ,@ret=@ret   output     
  7. select @ret      
  8. go  

【編輯推薦】

教您如何實(shí)現(xiàn)MySQL動(dòng)態(tài)視圖

SQL動(dòng)態(tài)查詢的示例

為您講解SQL動(dòng)態(tài)語句的語法

DB2數(shù)據(jù)庫對(duì)動(dòng)態(tài)游標(biāo)的使用

DB2動(dòng)態(tài)SQL的查看方法


文章題目:sql server中動(dòng)態(tài)sql語句的應(yīng)用
本文地址:http://m.5511xx.com/article/cdpeisg.html