当前位置: 代码迷 >> Sql Server >> 动态sql语句使用匹配查询?解决方法
  详细解决方案

动态sql语句使用匹配查询?解决方法

热度:8   发布时间:2016-04-27 17:07:47.0
动态sql语句使用匹配查询??
动态sql语句怎么样使用匹配查询?
SQL code
declare @store nvarchar(10)declare stofile cursor  forselect distinct imsul_store from maxmast.imsul where imsul_store not like '%F' order by 1open stofileFETCH NEXT FROM stofileINTO @storeWHILE @@FETCH_STATUS = 0 beginexec ('CREATE TABLE [email protected]+'(    [serno] [int] IDENTITY (1, 1)  NOT NULL ,    [item] [nvarchar] (15) ,    [store] [nvarchar] (10),    [account] nvarchar(10),    [bin] nvarchar(10),    [qty] float ) ON [PRIMARY]INSERT INTO [email protected]+'select imsul_item,imsul_store,imsul_account,imsul_bin,sum(imsul_balance) as qty from maxmast.imsulwhere imsul_store like [email protected]+'%''group by imsul_item, imsul_store,imsul_account,imsul_binorder by imsul_binselect * from [email protected]+'    ')FETCH NEXT FROM stofileINTO @storeendclose stofileDEALLOCATE stofile

上面的代码哪出问题了,[email protected],不能查出imsul_store [email protected]%的东西??

------解决方案--------------------
在游标中动态SQL?
试试

SQL code
exec ('CREATE TABLE [email protected]+'(    [serno] [int] IDENTITY (1, 1)  NOT NULL ,    [item] [nvarchar] (15) ,    [store] [nvarchar] (10),    [account] nvarchar(10),    [bin] nvarchar(10),    [qty] float ) ON [PRIMARY]INSERT INTO [email protected]+'select imsul_item,imsul_store,imsul_account,imsul_bin,sum(imsul_balance) as qty from maxmast.imsulwhere imsul_store like [email protected]+'%'''group by imsul_item, imsul_store,imsul_account,imsul_binorder by imsul_binselect * from [email protected]+')
------解决方案--------------------
'select imsul_item,imsul_store,imsul_account,imsul_bin,sum(imsul_balance) as qty from maxmast.imsul
where imsul_store like [email protected]+'%'''
[email protected],[email protected]
------解决方案--------------------
SQL code
create table #(    [serno] [int] IDENTITY (1, 1)  NOT NULL ,    [item] [nvarchar] (15) ,    [store] [nvarchar] (10),    [account] nvarchar(10),    [bin] nvarchar(10),    [qty] float )declare @store nvarchar(10)declare stofile cursor  forselect distinct imsul_store from maxmast.imsul where imsul_store not like '%F' order by 1open stofileFETCH NEXT FROM stofileINTO @storeWHILE @@FETCH_STATUS = 0 begintruncate table #/*print*/ exec ('INSERT INTO #select imsul_item,imsul_store,imsul_account,imsul_bin,sum(imsul_balance) as qty from maxmast.imsulwhere imsul_store like [email protected]+'%''group by imsul_item, imsul_store,imsul_account,imsul_binorder by imsul_bin')select * from #FETCH NEXT FROM stofileINTO @storeendclose stofileDEALLOCATE stofil
  相关解决方案