with textTable as
(
SELECT id,text as test1 FROM syscomments s WHERE id=OBJECT_ID('wage')--这个只有两行记录
),nameTable as(
Select id,name From SysColumns Where id=Object_Id('wage')--这个有N行记录
)select * from textTable inner join nameTable on textTable.id=nameTable.id
这样查询什么会有N行记录出来.我只想写成只有两行记录
我用left join也是一样

解决方案 »

  1.   

    这样查当然会有N行记录了,因为后面的有多条记录,而且ID是一样的.
    这样就行了:
    select a.id,a.text,b.name from syscomments a inner join syscolumns b on a.id=b.id where a.id=OBJECT_ID('wage')
      

  2.   


    nameTable as(
    Select id,name From SysColumns Where id=Object_Id('wage')--这个有N行记录
    )
    改为
    nameTable as(
    Select id,name From SysColumns Where id=Object_Id('wage') group by id,name--这个有N行记录
    )
      

  3.   

    或者改为
    nameTable as(
    Select distinct id,name From SysColumns Where id=Object_Id('wage')--这个有N行记录
    )
      

  4.   

    --前面说错了
    要这样
    nameTable as(
    Select id,name From SysColumns Where id=Object_Id('wage')--这个有N行记录
    )
    改为
    nameTable as(
    Select id,max(name) From SysColumns Where id=Object_Id('wage') group by id
    )
      

  5.   

    谢谢豆子.这样的话.会报错
    Msg 8155, Level 16, State 2, Line 1
    没有为 'nameTable' 的列 2 指定任何列。
      

  6.   

    你这个查询是没有意义的,前一查询所获得的是指定表所含的规则、默认值、触发器、CHECK 约束、DEFAULT 约束等的文本,而后一个查询是查询表的列名,你说这两个拼起来是什么?
    它们没有关联,除了是在同一个表中外.
      

  7.   

    一个是syscomments表,一个是SysColumns表.这是两个不同的表.但两个表是有ID关联的
      

  8.   

    此关联只表示它们是建在同一个表下面的,两者之间并没有什么关系,除非你硬从 text 里去找列名,但并不能保证它们depend的是同一个对象.
      

  9.   

    对,我就是要通过text字段去找列名.所以我就想出了这么一个办法.
      

  10.   

    不一定能找到的,这决定于在定义里是不是有那个列名,搞不好,一个定义里会有多个列名呢,比如 table constraint.
      

  11.   

    那反过来找呢?通过列名找text呢?
      

  12.   

    use master
    go
    create database test1
    go
    use test1
    go
    create table tb(id int identity(1,1) primary key,col1 int,col2 int,col3 int,col4 int,
    constraint checkcol3 check(col3=0 or col3=1)
    )
    go
    create trigger setcol3
    on tb
    INSTEAD OF insert
    as
    begin
    insert into tb select col1,col2,col3,(case when col3=0 then col1+col2 else col1-col2 end)as col4 from inserted
    end
    go
    select b.name,a.text from syscomments a inner join syscolumns b on charindex(b.name,a.text)>0
    /*
    name                                                                                                                             text
    -------------------------------------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    col3                                                                                                                             ([col3]=(0) OR [col3]=(1))
    col1                                                                                                                             create trigger setcol3
    on tb
    INSTEAD OF insert
    as
    begin
    insert into tb select col1,col2,col3,(case when col3=0 then col1+col2 else col1-col2 end)as col4 from inserted
    end
    col2                                                                                                                             create trigger setcol3
    on tb
    INSTEAD OF insert
    as
    begin
    insert into tb select col1,col2,col3,(case when col3=0 then col1+col2 else col1-col2 end)as col4 from inserted
    end
    col3                                                                                                                             create trigger setcol3
    on tb
    INSTEAD OF insert
    as
    begin
    insert into tb select col1,col2,col3,(case when col3=0 then col1+col2 else col1-col2 end)as col4 from inserted
    end
    col4                                                                                                                             create trigger setcol3
    on tb
    INSTEAD OF insert
    as
    begin
    insert into tb select col1,col2,col3,(case when col3=0 then col1+col2 else col1-col2 end)as col4 from inserted
    end(5 行受影响)*/
    go
    use master
    go
    drop database test1
      

  13.   

    nameTable as(
    Select id,max(name) as name From SysColumns Where id=Object_Id('wage') group by id
    )