Solved

SQL Cursor - How to concatenate a value

Posted on 2013-01-16
2
364 Views
Last Modified: 2013-01-20
I have a SQL Stored Procedure that validates a form.  One section of the validation needs to verify a keyword(s) was selected.  Depending on the users form there could be 3 to 25 keywords to confirm.

The idea is that @KEYWORDCATEGORY1 and @KEYWORDCATEGORY1_REQ need to increment to @KEYWORDCATEGORY2, @KEYWORDCATEGORY3 and @KEYWORDCATEGORY2_REQ, @KEYWORDCATEGORY3_REQ and so on.  

I am asuming I can do this by concatenating a variable  to @KEYWORDCATEGORY such as @KEYWORDCATEGORY3 + @VALUE or @KEYWORDCATEGORY+ @VALUE + '_REQ' where @VALUE woud start at 1.  Can this be done?



/*VERIFY KEYWORDS*/
DECLARE KEYWORDS_CRSR CURSOR STATIC LOCAL FOR
SELECT
           [KeywordId]
FROM
           [dbo].[TableName]
WHERE
           [User_Id] = @ID

OPEN KEYWORDS_CRSR

If @@CURSOR_ROWS > 0
BEGIN
     FETCH NEXT FROM KEYWORDS_CRSR INTO
                  @KEYWORDCATEGORY1

WHILE @@FETCH_STATUS = 0
BEGIN

    /*@KEYWORDCATEGORY1_REQ*/
    IF @KEYWORDCATEGORY1_REQ = 1
    BEGIN
             IF @KEYWORDCATEGORY1 IS NULL
             BEGIN
           SET @ACCEPTED = 0                  
                       SET @RESPONSE = @RESPONSE + '-A Keyword for Category l must be selected.<br />'
            END
             END

             FETCH NEXT FROM KEYWORDS_CRSR INTO
                          @KEYWORDCATEGORY1
END


THANKS!!
0
Comment
Question by:skinsfan99
2 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 38782118
no, this cannot be done "like this"

but you could do this in a plain statement, without cursor, eventually.

can you clarify the data in the table, and what would the full output be?

thanks
0
 

Author Closing Comment

by:skinsfan99
ID: 38798147
Thank you!!
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question