Insert into question

  • Hello

    I need to insert records into a table, like this

    TABLE BC

    REF | Design | code

    001 | teste |0001

    001 | teste |0002

    001 |teste |00xx , until it reaches 9999

    with insert into command i only can insert a record of a time, is there another like in a cicle until code=9999

    Thanks in advance

  • Hi,

    There's no need for a loop. You just need to use a Tally Table. To know what it is and how it replaces a loop, read the following: http://www.sqlservercentral.com/articles/T-SQL/62867/

    With your tally table, your code would end like this:

    INSERT INTO BC

    SELECT '001',

    'teste',

    RIGHT( '000' + CAST( n AS varchar(4)), 4)

    FROM dbo.Tally

    WHERE n <= 9999

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • hi

    After creating the tally table, used this, but gives me a error on the select

    INSERT INTO BC (ref,design,codigo)

    values ("001","teste", SELECT '001',

    'teste',

    RIGHT( '000' + CAST( n AS varchar(4)), 4)

    FROM dbo.Tally

    WHERE n <= 9999)

    thanks for you reply

  • That's because you're still using the VALUES clause when you should only use the SELECT.

    For more information on INSERT INTO: http://technet.microsoft.com/en-us/library/dd776381(v=sql.105).aspx Check the part Inserting data from other tables

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • i put

    INSERT INTO BC

    SELECT '001','teste', RIGHT( '000' + CAST( n AS varchar(4)), 4)

    FROM dbo.Tally

    WHERE n <= 9999

    and returns this error msg

    Erro: Connectivity error: [Microsoft][ODBC SQL Server Driver][SQL Server]Invalid object name 'BC'.

    table bc exists

  • if i do

    select * from tally

    returns all the numbers ok 1,2,....11000

  • You need to be sure you're in the correct instance.

    To be sure you're inserting on the right table, you could use a 3 part name: [db_name].[schema_name].[table_name]

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • ok many thanks, works fine, e needed to create table tally on my database not in tempdb like in the example

  • You can create it on temdb and use it from any other database by using the 3-part name (tempdb.dbo.Tally)

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2

Viewing 9 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic. Login to reply