Looping SQL SELECT and INSERT INTO query

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alec Christie

    Looping SQL SELECT and INSERT INTO query

    Hi All,

    I have two tables, Clients (ClientID,Clien tIndustry,Clien tCounty) and
    EventStatus (StatusID,Event ID,ClientID)
    I have a form that Creates a new Event with the following fields:

    EventID (Text)
    ClientIndustry (ListBox values Media,IT,Sport)
    ClientCounty (ListBox values Norfolk,Leicste rshire,Nottingh am)

    I want to create an SQL loop that will search the Clients table, and
    for every client that has the selected industry/ies and County/ies
    values, creates an entry into the EventStatus table with the EventID
    and ClientID entered.

    Any idea's?

    Thanks in Advance!
  • Nicole

    #2
    Re: Looping SQL SELECT and INSERT INTO query

    alec_e_christie @hotmail.com (Alec Christie) wrote in message news:<6f872380. 0408050347.f04e da4@posting.goo gle.com>...[color=blue]
    > Hi All,
    >
    > I have two tables, Clients (ClientID,Clien tIndustry,Clien tCounty) and
    > EventStatus (StatusID,Event ID,ClientID)
    > I have a form that Creates a new Event with the following fields:
    >
    > EventID (Text)
    > ClientIndustry (ListBox values Media,IT,Sport)
    > ClientCounty (ListBox values Norfolk,Leicste rshire,Nottingh am)
    >
    > I want to create an SQL loop that will search the Clients table, and
    > for every client that has the selected industry/ies and County/ies
    > values, creates an entry into the EventStatus table with the EventID
    > and ClientID entered.
    >
    > Any idea's?
    >
    > Thanks in Advance![/color]

    Air code; doesn't do exactly what you want but should get you in the
    ballpark and you can modify to your situation:

    declare @clientid numeric, @eventid nvarchar(25)
    declare events cursor for

    select clientid, industry, country from dbo.client
    where ClientIndustry = 'industry_name' and ClientCountry =
    'country_name'

    open events
    fetch next from events
    into @clientid, @eventID
    while @@fetch_Status = 0
    begin
    insert into event status (clientid, eventid)
    values (@clientid, @eventID)

    fetch next from events
    into @clientid, @eventID
    end
    close events
    deallocate events

    Comment

    • Alec Christie

      #3
      Re: Looping SQL SELECT and INSERT INTO query

      Nicole,

      Thanks for the tips, I hadnt looked at the it that way, took me few
      hours to get it going, but its all worked, nice one!

      Alec



      *** Sent via Developersdex http://www.developersdex.com ***
      Don't just participate in USENET...get rewarded for it!

      Comment

      • Nicole

        #4
        Re: Looping SQL SELECT and INSERT INTO query

        > Nicole,[color=blue]
        >
        > Thanks for the tips, I hadnt looked at the it that way, took me few
        > hours to get it going, but its all worked, nice one!
        >
        > Alec[/color]

        Glad it helped. Once you get the hang of loops you find all sorts of
        uses for these nifty helpers.

        Comment

        Working...