例如 我要在UserInfo 表里面的ui_account 字段 插入 test加上主键(ui_id)合并后的字符串(如test1, test2),SQL语句应该怎么写?谢谢!

解决方案 »

  1.   

    比如我要插入1000条记录,ui_account是 test1, test2 。 test1000,就是test + ui_id, 用sql语句应该怎么写呢?
      

  2.   

    是不是/****************************************************************************************************************************************************** 
    合并分拆表数据 整理人:中国风(Roy) 日期:2008.06.06 
    ******************************************************************************************************************************************************/ --> --> (Roy)生成測試數據 if not object_id('Tab') is null 
        drop table Tab 
    Go 
    Create table Tab([Col1] int,[Col2] nvarchar(1)) 
    Insert Tab 
    select 1,N'a' union all 
    select 1,N'b' union all 
    select 1,N'c' union all 
    select 2,N'd' union all 
    select 2,N'e' union all 
    select 3,N'f' 
    Go 合并表: SQL2000用函数: go 
    if object_id('F_Str') is not null 
        drop function F_Str 
    go 
    create function F_Str(@Col1 int) 
    returns nvarchar(100) 
    as 
    begin 
        declare @S nvarchar(100) 
        select @S=isnull(@S+',','')+Col2 from Tab where Col1=@Col1 
        return @S 
    end 
    go 
    Select distinct Col1,Col2=dbo.F_Str(Col1) from Tab go SQL2005用XML: 方法1: select 
        a.Col1,Col2=stuff(b.Col2.value('/R[1]','nvarchar(max)'),1,1,'') 
    from 
        (select distinct COl1 from Tab) a 
    Cross apply 
        (select COl2=(select N','+Col2 from Tab where Col1=a.COl1 For XML PATH(''), ROOT('R'), TYPE))b 方法2: select 
        a.Col1,COl2=replace(b.Col2.value('/Tab[1]','nvarchar(max)'),char(44)+char(32),char(44)) 
    from 
        (select distinct COl1 from Tab) a 
    cross apply 
        (select Col2=(select COl2 from Tab  where COl1=a.COl1 FOR XML AUTO, TYPE) 
                    .query(' <Tab> 
                    {for $i in /Tab[position() <last()]/@COl2 return concat(string($i),",")} 
                    {concat("",string(/Tab[last()]/@COl2))} 
                    </Tab>') 
                    )b SQL05用CTE: ;with roy as(select Col1,Col2,row=row_number()over(partition by COl1 order by COl1) from Tab) 
    ,Roy2 as 
    (select COl1,cast(COl2 as nvarchar(100))COl2,row from Roy where row=1 
    union all 
    select a.Col1,cast(b.COl2+','+a.COl2 as nvarchar(100)),a.row from Roy a join Roy2 b on a.COl1=b.COl1 and a.row=b.row+1) 
    select Col1,Col2 from Roy2 a where row=(select max(row) from roy where Col1=a.COl1) order by Col1 option (MAXRECURSION 0) 
    生成结果: 
    /* 
    Col1        COl2 
    ----------- ------------ 
    1          a,b,c 
    2          d,e 
    3          f (3 行受影响) 
    */ 
      

  3.   

    在学习中遇到这个问题 
    数据库里有编号字段 
    BH00001 
    BH00002 
    BH00003 
    BH00004 
    如何实现自动增长 --下面的代码生成长度为8的编号,编号以BH开头,其余6位为流水号。
    --得到新编号的函数
    CREATE FUNCTION f_NextBH()
    RETURNS char(8)
    AS
    BEGIN
        RETURN(SELECT 'BH'+RIGHT(1000001+ISNULL(RIGHT(MAX(BH),6),0),6) FROM tb WITH(XLOCK,PAGLOCK))
    END
    GO--在表中应用函数
    CREATE TABLE tb(
    BH char(8) PRIMARY KEY DEFAULT dbo.f_NextBH(),
    col int)--插入资料
    BEGIN TRAN
        INSERT tb(col) VALUES(1)
        INSERT tb(col) VALUES(2)
        INSERT tb(col) VALUES(3)
        DELETE tb WHERE col=3
        INSERT tb(col) VALUES(4)
        INSERT tb(BH,col) VALUES(dbo.f_NextBH(),14)
    COMMIT TRAN--显示结果
    SELECT * FROM tb
    /*--结果
    BH         col 
    ---------------- ----------- 
    BH000001  1
    BH000002  2
    BH000003  4
    BH000004  14
    --*/ create table tb
    (id int identity,
    name varchar(10),
    code as 'BH'+right('0000'+cast(id as varchar),5))
    go
    insert tb(name) select 'A'
    union all select 'B'
    union all select 'C'
    union all select 'D'select * from tbdrop table tb/*
    id          name       code         
    ----------- ---------- ------------ 
    1           A          BH00001
    2           B          BH00002
    3           C          BH00003
    4           D          BH00004(所影响的行数为 4 行)
    */
      

  4.   

    userinfo 就两个字段 ui_id 主键 自增 和 ui_account varchar(50) 我要插入1000条记录,ui_account是 test1, test2 。 test1000,应该怎么写语句来执行呢?
      

  5.   

    我要插入1000 难道要union 1000次 么
      

  6.   

    是更新吧:
    update userinfo set ui_account=test1+test2+ltrim(ui_id)
      

  7.   

    insert into userinfo 
    select test+ltrim(number+1) from master..spt_values where type='p' and number<1000
      

  8.   

    create table tb
    (ui_id int identity,
    ui_account as 'test'+right('0000'+cast(id as varchar(100)),4))
      

  9.   

    完全不了我的意思啊,我就是想在userinfo 表里面加1000个帐号,分别从test1 - test1000,就是不想手动一个一个输入,想执行sql语句实现啊。