I'm not sure what you mean by "assign a string to a column name". Do you mean you want to dynamically select the columns based on a parameter? For example:
If @parameter = 'Jack'
Begin
Set @column_name = 'FirstName'
End
Else
Begin
Set @column_name = 'LastName'
End
Select
@column_name
From
table1
If this is what you want to do, you need to use dynamic SQL like this:
Declare @sql varchar(8000)
Set @sql = 'Select '
If @parameter = 'Jack'
Begin
Set @sql = @sql + 'FirstName '
End
Else
Begin
Set @sql = @sql + 'LastName '
End
Set @sql = @sql + 'From table1'
Exec (@sql)
Have never really used Break and Continue in SQL Server. Look them up in Books on Line. I do know that if you do the Break within the cursor loop it will stop iterating through the cursor when you hit it.
Best Practices in SQL Server are to use cursors as little as possible as they are more resource intensive and slower than set-based solutions. If you were to post what you need to accomplish with the table schemas and some data someone may be able help you find a better solution than the cursor.
See this article, http://www.sqlservercentral.com/articles/Best+Practices/61537/ for advice on posting that will allow for fast and accurate anwers.
Jack Corbett
Consultant - Straight Path Solutions
Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question