Results 1 to 6 of 6

Thread: help me for a small query in sql server 2000

  1. #1
    Join Date
    Jul 2006
    Posts
    29

    Thumbs up help me for a small query in sql server 2000

    I am having a problem making a SQL query .. plz help me sort it out . It is drafted in the attachment.

    With lots of thanks
    Attached Files Attached Files
    Last edited by zathrone; 09-04-2006 at 10:05 AM.

  2. #2
    Join Date
    Sep 2002
    Posts
    5,938
    T-sql doesn't support array and you can't use table variable in sp, but you can save value of taxtboxes to temp table and let your sp get them from that temp table. In your sp, you can either build dynamic sql to get all rows for all textboxes or use cursor to get rows for each textbox.

  3. #3
    Join Date
    Jul 2006
    Posts
    29

    Thanks

    Quote Originally Posted by rmiao
    T-sql doesn't support array and you can't use table variable in sp, but you can save value of taxtboxes to temp table and let your sp get them from that temp table. In your sp, you can either build dynamic sql to get all rows for all textboxes or use cursor to get rows for each textbox.
    Thanks for your reply!
    can u plz provide me with an example of it , i mean the one u have mentioned?

    [If any other ideas, plz post!)
    Last edited by zathrone; 08-29-2006 at 11:12 AM. Reason: additional comment

  4. #4
    Join Date
    Sep 2005
    Posts
    168
    --modify the procedure according to your database table
    --exec records_per_person @break_str = '^', @person_str = 'John^Nick^George^James'

    CREATE PROCEDURE records_per_person
    @break_str VARCHAR(2) = NULL,
    @person_str NVARCHAR(1000) = NULL --in form of name1[breaking character]name2[breaking character]
    AS

    IF @break_str IS NULL
    RETURN

    DECLARE @runner SMALLINT

    --table variable holding input names
    DECLARE @mynames TABLE (the_name NVARCHAR(100) NULL)

    --add trailing breaking string/s
    SELECT @person_str = @person_str + @break_str
    WHERE RIGHT(@person_str, LEN(@break_str)) <> @break_str

    --run through input names
    SELECT @runner = 1

    WHILE @runner <= LEN(@person_str)
    BEGIN
    --add name to table variable
    INSERT INTO @mynames (the_name)
    VALUES (SUBSTRING(@person_str, @runner,CHARINDEX(@break_str, @person_str, @runner+1)-@runner) )

    --move to next breaking string
    SELECT @runner = CHARINDEX(@break_str, @person_str, @runner+1) + 1
    END

    SELECT my.the_name AS person_name, COUNT(tbl.person_name) AS num_of_records
    FROM @mynames my
    LEFT JOIN db_table tbl ON tbl.person_name = my.the_name
    GROUP BY my.the_name

  5. #5
    Join Date
    Jul 2006
    Posts
    29

    thanks a lot

    Quote Originally Posted by mikr0s
    --modify the procedure according to your database table
    --exec records_per_person @break_str = '^', @person_str = 'John^Nick^George^James'

    CREATE PROCEDURE records_per_person
    @break_str VARCHAR(2) = NULL,
    @person_str NVARCHAR(1000) = NULL --in form of name1[breaking character]name2[breaking character]
    AS

    IF @break_str IS NULL
    RETURN

    DECLARE @runner SMALLINT

    --table variable holding input names
    DECLARE @mynames TABLE (the_name NVARCHAR(100) NULL)

    --add trailing breaking string/s
    SELECT @person_str = @person_str + @break_str
    WHERE RIGHT(@person_str, LEN(@break_str)) <> @break_str

    --run through input names
    SELECT @runner = 1

    WHILE @runner <= LEN(@person_str)
    BEGIN
    --add name to table variable
    INSERT INTO @mynames (the_name)
    VALUES (SUBSTRING(@person_str, @runner,CHARINDEX(@break_str, @person_str, @runner+1)-@runner) )

    --move to next breaking string
    SELECT @runner = CHARINDEX(@break_str, @person_str, @runner+1) + 1
    END

    SELECT my.the_name AS person_name, COUNT(tbl.person_name) AS num_of_records
    FROM @mynames my
    LEFT JOIN db_table tbl ON tbl.person_name = my.the_name
    GROUP BY my.the_name

    Thanks a lot for ur nice effort ! I really appreciate ur reply.

  6. #6
    Join Date
    Jul 2006
    Posts
    29

    update

    [



    Thanks For The Reply


    ]
    Last edited by zathrone; 09-04-2006 at 10:33 AM. Reason: problem solved

Posting Permissions

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