declare  cursor1 cursor for select d.name,a.name from syscolumns a, systypes b,sysobjects d where a.xtype=b.xusertype and a.id=d.id and d.xtype='U' and b.name like '%char'
declare
@i varchar(1000),@j varchar(1000)
open cursor1
fetch cursor1 into @i,@j
while @@fetch_status=0
begin
  exec('select * from '+@i+' where '+@j+' =''你的参数''')
  fetch cursor1 into @i
end
close cursor1
deallocate cursor1

解决方案 »

  1.   

    declare  cursor1 cursor for select d.name,a.name from syscolumns a, systypes b,sysobjects d where a.xtype=b.xusertype and a.id=d.id and d.xtype='U' and b.name like '%char'
    declare
    @i varchar(1000),@j varchar(1000)
    open cursor1
    fetch cursor1 into @i,@j
    while @@fetch_status=0
    begin
      exec('if exists(select 1 from '+@i+' where '+@j+' =''你的参数'') select '''+@i+''' 表名,'''+@j+''' 列名')
      fetch cursor1 into @i,@j
    end
    close cursor1
    deallocate cursor1
      

  2.   

    大力你好:
      报错
      
    Server: Msg 16924, Level 16, State 1, Line 10
    Cursorfetch: The number of variables declared in the INTO list must match that of selected columns.
      

  3.   

    不可会吧你试试:
    declare  cursor1 cursor for select d.name,a.name from syscolumns a, systypes b,sysobjects d where a.xtype=b.xusertype and a.id=d.id and d.xtype='U' and b.name like '%char'
    declare
    @i varchar(1000),@j varchar(1000)
    open cursor1
    fetch cursor1 into @i,@j
    while @@fetch_status=0
    begin
      exec('if exists(select 1 from '+@i+' where '+@j+' like ''%a%'') select '''+@i+''' 表名,'''+@j+''' 列名')
      fetch cursor1 into @i,@j
    end
    close cursor1
    deallocate cursor1