通过sql语句找到列名字为‘date_3’的那一列对比的数据库字段不是固定的,要在date_1到date_10中筛选

来源:互联网 发布:360浏览器优化版 编辑:程序博客网 时间:2024/05/21 19:42

drop table tb
create table tb
(
col
varchar(50)
)
create table Plan_Orignal_Table1
(
date_3
varchar(50)
)
insert into tb values('date_1')
insert into tb values('date_2')
insert into tb values('date_3')
insert into tb values('date_4')
insert into tb values('date_5')
insert into tb values('date_6')
insert into tb values('date_7')
insert into tb values('date_8')
insert into tb values('date_9')
insert into tb values('date_10')


declare @columns varchar(20)
declare @result varchar(20)
DECLARE my_Cursor Cursor  FOR
SELECT col
FROM tb 
OPEN my_Cursor
FETCH NEXT FROM my_Cursor
into @columns
WHILE @@FETCH_STATUS = 0
begin
 
IF EXISTS (select * from syscolumns where id=object_id('Plan_Orignal_Table1') and name =@columns)
  
begin
 
set @result=@columns
 
end
  
FETCH NEXT FROM my_Cursor into @columns
end
CLOSE my_Cursor
DEALLOCATE my_Cursor

select @result


原创粉丝点击