Results 1 to 4 of 4

Thread: Problem In Cursor

  1. #1

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Question Problem In Cursor

    Hi all
    I am facing some problem in cursor.


    Code:
    ALTER PROCEDURE [dbo].[Product_Despatch_Return]
    @Search_Transaction_ID int,
    @Search_Product_Name nvarchar(50)
    AS
    BEGIN
    	SET NOCOUNT ON;
    	DECLARE 	@QueryString nvarchar(MAX)
    
    SET @QueryString='SELECT Product_Transction.Transaction_ID AS Transaction_ID, Product_Transction.Transaction_Date AS Transaction_Date,Product_Transaction_Details.Product_Id AS Product_Id, Product_Master.Product_Name AS Product_Name,Product_Transaction_Details.Qty AS OrderQuantity,Product_Transaction_Details.Details_Id AS Product_Transaction_Details_Id FROM Product_Transction INNER JOIN  Product_Transaction_Details ON Product_Transction.Transaction_ID = Product_Transaction_Details.Transaction_Id INNER JOIN  Product_Master ON Product_Transaction_Details.Product_Id = Product_Master.Product_ID WHERE Product_Transction.Is_Deleted=0 AND Product_Transaction_Details.Is_Deleted=0 '
    	-- CHECKING THE @Search_Transaction_ID
    	IF NOT (@Search_Transaction_ID=NULL OR @Search_Transaction_ID=0)
    	BEGIN
    		SET @QueryString=@QueryString+ ' Product_Transction.Transaction_ID='+CAST(@Search_Transaction_ID AS NVARCHAR)
    	END
    	-- CHECKING THE @Search_Product_Name
    	IF NOT (@Search_Product_Name=NULL OR @Search_Product_Name='')
    	BEGIN
    		SET @QueryString=@QueryString+ ' Product_Master.Product_Name='''+@Search_Product_Name+''''
    	END
    
    	DECLARE C CURSOR FOR @QueryString ' Error is Here
    	OPEN C
    I am facing error in the next line.
    DECLARE C CURSOR FOR @QueryString ' Error is Here
    Incorrect syntax near '@QueryString
    Last edited by shakti5385; Dec 20th, 2007 at 12:09 AM.

  2. #2
    Frenzied Member brin351's Avatar
    Join Date
    Mar 2007
    Location
    Land Down Under
    Posts
    1,293

    Re: Problem In Cursor

    guessing this is sql server? could the problem be a few lines up at (@Search_Transaction_ID=NULL or... should be (@Search_Transaction_ID IS NULL or ....

    Just a thought...

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

    Re: Problem In Cursor

    And what is making you think that the CURSOR can be set to a variable string of SQL?

    I did this and it worked

    Code:
    declare c cursor for select * from tcs_t
    deallocate c
    But this gets a syntax error

    Code:
    declare @str varchar(100)
    set @str='select * from tcs_t'
    declare c cursor for @str
    deallocate c
    You will have to change your logic so that the WHERE clause has OR's and what not...

    *** 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

  4. #4
    Lively Member
    Join Date
    Nov 2005
    Posts
    104

    Re: Problem In Cursor

    Quote Originally Posted by szlamany
    And what is making you think that the CURSOR can be set to a variable string of SQL?

    I did this and it worked

    Code:
    declare c cursor for select * from tcs_t
    deallocate c
    But this gets a syntax error

    Code:
    declare @str varchar(100)
    set @str='select * from tcs_t'
    declare c cursor for @str
    deallocate c
    You will have to change your logic so that the WHERE clause has OR's and what not...
    Learn something new every day!

    Also, your if conditions don't append an 'AND' or 'OR' so that won't work as a regular query anyways... I think.

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