Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 WHILE EXISTS (SELECT @VAR = name .....

Author  Topic 

iarp
Starting Member

2 Posts

Posted - 2012-09-06 : 19:11:01
I'm trying to find out if it's possible to assign a value to a variable from within the WHILE EXISTS statement, saving me from having to re-call the query again inside the WHILE loop.


declare @VAR varchar(50), @FieldName varchar(50)

SET @FieldName = 'FULL_NAME'

WHILE EXISTS (SELECT @VAR c_obj.name as CONSTRAINT_NAME from sysobjects c_obj
JOIN syscomments com on c_obj.id = com.id
JOIN sysobjects t_obj on c_obj.parent_obj = t_obj.id
JOIN sysconstraints con on c_obj.id = con.constid
JOIN syscolumns col on t_obj.id = col.id
AND con.colid = col.colid
WHERE
c_obj.uid = user_id()
AND c_obj.xtype = 'D'
AND t_obj.name='CONTACTS'
and col.name=@FieldName)
.....


The purpose to this is to get all constraints for the @FieldName value, and my current script has the above while script, but within the loop itself i redo that query exactly the same just so i can grab the value. It would be nice if i could grab it from the while statement.

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-09-06 : 19:39:54
What you have posted generates a syntax error, so I didn't quite follow what you were attempting there. It may not even be necessary to check the while exists and then grab it - you perhaps could do one assignment and check to see if that value is null.

If you post the part after the while exists check, people on the forum may be able to offer better suggestions.
Go to Top of Page

iarp
Starting Member

2 Posts

Posted - 2012-09-06 : 19:48:06
[code]
declare @VAR varchar(50), @FieldName varchar(50), @alterStatement nvarchar(max)

SET @FieldName = 'FULL_NAME'

WHILE EXISTS (SELECT c_obj.name as CONSTRAINT_NAME from sysobjects c_obj
JOIN syscomments com on c_obj.id = com.id JOIN sysobjects t_obj on c_obj.parent_obj = t_obj.id
JOIN sysconstraints con on c_obj.id = con.constid JOIN syscolumns col on t_obj.id = col.id AND con.colid = col.colid
WHERE
c_obj.uid = user_id() AND c_obj.xtype = 'D' AND t_obj.name='CONTACTS' and col.name=@FieldName)
BEGIN
SELECT @alterStatement = 'ALTER TABLE CONTACTS DROP CONSTRAINT ' + c_obj.name from sysobjects c_obj
join syscomments com on c_obj.id = com.id join sysobjects t_obj on c_obj.parent_obj = t_obj.id join sysconstraints con on c_obj.id = con.constid
join syscolumns col on t_obj.id = col.id and con.colid = col.colid where
c_obj.uid = user_id() and c_obj.xtype = 'D' and t_obj.name='CONTACTS' and col.name=@FieldName

EXEC sp_executesql @alterStatement
END
[/code]

I'm wanting to get rid of the need to do the entire select statement again within the loop.

CONTACTS is the table name.
@FieldName is the column.
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-09-07 : 07:40:00
What you have is probably just as good as anything else. If you do want to avoid the two selects, you can assign variable and see if the row count is greater than 0. - for example like this:
DECLARE @rc INT = 1;
WHILE (@rc > 0 )
BEGIN
SELECT TOP (1) @alterStatement = 'ALTER TABLE CONTACTS DROP CONSTRAINT ' + c_obj.name from sysobjects c_obj
join syscomments com on c_obj.id = com.id join sysobjects t_obj on c_obj.parent_obj = t_obj.id join sysconstraints con on c_obj.id = con.constid
join syscolumns col on t_obj.id = col.id and con.colid = col.colid where
c_obj.uid = user_id() and c_obj.xtype = 'D' and t_obj.name='CONTACTS' and col.name=@FieldName

@rc = @@ROWCOUNT;
EXEC sp_executesql @alterStatement
END
Go to Top of Page
   

- Advertisement -