Results 1 to 8 of 8

Thread: SQL Stored Procedure Question/Looping Records

  1. #1

    Thread Starter
    Lively Member smilbuta's Avatar
    Join Date
    Apr 2005
    Location
    Orlando
    Posts
    104

    SQL Stored Procedure Question/Looping Records

    I have Table A with 3 cloumns x,y,z Varchars.(data is a first middle lastname stuff)
    I have Table b that is supposed to Store the Concatenated result of (xyz)

    I have a stored procedure that is supposed to move through Table A and read the 3 columns then insert the result to table b. It works for 1 record only then stops, I am unsure how to get it to do more than one record.

    Code:
    CREATE  PROCEDURE namecat @newname nvarchar(50)
    AS
    
    	select  @newname = (X + Y + Z) from TableA
    	Insert into  TableB (xyz) Values (@newname)
    GO
    Sorry for the Newish question but im thiking a Cursor is needed?

  2. #2
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: SQL Stored Procedure Question/Looping Records

    Cursors should be a last resort and in this case they are not needed.

    Insert Into TableB (xyz) Select x + ', ' + y + ' ' + z From TableA

    What is the purpose of the @NewName parameter?

  3. #3

    Thread Starter
    Lively Member smilbuta's Avatar
    Join Date
    Apr 2005
    Location
    Orlando
    Posts
    104

    Re: SQL Stored Procedure Question/Looping Records

    The new name parameter was how i was going to pass the cont cat variables to the insert statment.

  4. #4
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: SQL Stored Procedure Question/Looping Records

    Quote Originally Posted by smilbuta
    The new name parameter was how i was going to pass the cont cat variables to the insert statment.
    You are thinking like a programmer - and T-SQL needs you to lose that old iterative/logic processing mindset.

    You need to try to see how an entire "grid" of data can be prepared - as in the SELECT statement that BRUCEVDE showed you - and that grid will effortlessly pass into an INSERT statement.

    All in one shot - that's the beauty of set-based logic and SQL...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  5. #5
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: SQL Stored Procedure Question/Looping Records

    Code:
    insert into b values
         b.xyz = a.xyz
    from 
         (select x + y + z xyz
          from a)  a

    edit:
    first idea
    basically you use a subquery for it but i have not tested it wait...

  6. #6
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: SQL Stored Procedure Question/Looping Records

    oceane...

    BRUCEVDE's syntax is fine - INSERT ... SELECT ... will work in MS SQL server. Probably in ACCESS as well.

    I do not believe that you need to so carefully wrap it in ()'s to indicate it's sub-query nature or that you are required to alias it.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  7. #7
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: SQL Stored Procedure Question/Looping Records

    hehhe yep there's a bug in it .. i tested this new one

    Code:
    DECLARE @a TABLE(x char(1), y char(2), z char(3))
    DECLARE @b TABLE(xyz varchar(5))
    
    insert into @a values('a', 'b', 'c')
    insert into @a values('d', 'e', 'f')
    
    
    select x + y + z xyz from
       @a
    
    -- this is the only statement needed
    insert into @b
      select x + y + z xyz from
       @a
    his problem was because he only gets the first record, this one gets all in one statement.. though not sure in access. but here is the t-sql

  8. #8
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: SQL Stored Procedure Question/Looping Records

    LOLz did not see that one sorry bruce

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width