select newid(),* from table

解决方案 »

  1.   

    insert into table4([id],[name]) values(newid(),'cutesun')
      

  2.   

    创建 uniqueidentifier 类型的唯一值。
      

  3.   

    我这里有一个可以随机生成小写字母字符串的方法,length是字符串的长度public string CreateRandomString(int length)
    {
    Encoding encoding = Encoding.ASCII;
    byte[] buf = new Byte[encoding.GetByteCount(new string(' ', length))];
    for (;;)
    {
    Random r = new Random();
    for(int i=0;i<length;i++)
    {
    int ASCIINum = r.Next(97,122);
    buf[i] = (byte)ASCIINum;
    }
    try
    {
    return new string(encoding.GetChars(buf));
    }
    catch(Exception)
    {
    }
    }
    }
      

  4.   

    下面的示例创建具有 uniqueidentifier 数据类型的 cust 表,并使用 NEWID 将默认值填充到表中。为 NEWID() 赋默认值时,每个新行和现有行均具有 cust_id 列的唯一值。 -- Creating a table using NEWID for uniqueidentifier data type. 
    CREATE TABLE cust
    (
     cust_id uniqueidentifier NOT NULL
       DEFAULT newid(),
     company varchar(30) NOT NULL,
     contact_name varchar(60) NOT NULL, 
     address varchar(30) NOT NULL, 
     city varchar(30) NOT NULL,
     state_province varchar(10) NULL,
     postal_code varchar(10) NOT NULL, 
     country varchar(20) NOT NULL, 
     telephone varchar(15) NOT NULL,
     fax varchar(15) NULL
    )
    GO
    -- Inserting data into cust table.
    INSERT cust
    (cust_id, company, contact_name, address, city, state_province, 
     postal_code, country, telephone, fax)
    VALUES
    (newid(), 'Wartian Herkku', 'Pirkko Koskitalo', 'Torikatu 38', 'Oulu', NULL,
     '90110', 'Finland', '981-443655', '981-443655')
    INSERT cust
    (cust_id, company, contact_name, address, city, state_province,
    postal_code, country, telephone, fax)
    VALUES 
    (newid(), 'Wellington Importadora', 'Paula Parente', 'Rua do Mercado, 12', 'Resende', 'SP',
     '08737-363', 'Brazil', '(14) 555-8122', '')
    INSERT cust
    (cust_id, company, contact_name, address, city, state_province,
     postal_code, country, telephone, fax)
    VALUES
    (newid(), 'Cactus Comidas para Ilevar', 'Patricio Simpson', 'Cerrito 333', 'Buenos Aires', NULL, 
     '1010', 'Argentina', '(1) 135-5555', '(1) 135-4892')
    INSERT cust
    (cust_id, company, contact_name, address, city, state_province,
     postal_code, country, telephone, fax)
    VALUES 
    (newid(), 'Ernst Handel', 'Roland Mendel', 'Kirchgasse 6', 'Graz', NULL,
     '8010', 'Austria', '7675-3425', '7675-3426')
    INSERT cust
    (cust_id, company, contact_name, address, city, state_province,
     postal_code, country, telephone, fax)
    VALUES 
    (newid(), 'Maison Dewey', 'Catherine Dewey', 'Rue Joseph-Bens 532', 'Bruxelles', NULL,
     'B-1180', 'Belgium', '(02) 201 24 67', '(02) 201 24 68')
    GO
      

  5.   

    要是取数子,就改为
    int ASCIINum = r.Next(48,57);
    要是去大写字母,就改为
    int ASCIINum = r.Next(65,90);