assigning values to a parameter of a sql statement

  • i have table table1 with two columns col1,col2

    my statement is insert table1 values('dummy',xyz)

    where xyz is a variable and i want to put some values in a loop for xyz should be replaced by 1...10 i.e 11 times whatever;

    How do i do that? replace is one option?

    ciao!

  • nabajyoti.b (3/19/2009)


    i have table table1 with two columns col1,col2

    my statement is insert table1 values('dummy',xyz)

    where xyz is a variable and i want to put some values in a loop for xyz should be replaced by 1...10 i.e 11 times whatever;

    How do i do that? replace is one option?

    ciao!

    Parameters in TSQL would look something like this:

    @xyz

    To create and assign it within a query your code looks like this:

    DECLARE @xyz varchar(50);

    SET @xyz = 'My Value';

    INSERT INTO TABLE1

    (col1

    ,col2)

    VALUES

    ('dummy'

    ,@xyz)

    That's for a single row insert. For multiple rows, generally you shouldn't use a variable like this. Instead you would use a comma delimited list which you can pivot using a tally table or XML which you can query from. This way you're focusing on using sets of data, not loops.

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

Viewing 2 posts - 1 through 1 (of 1 total)

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