SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 WHILE EXISTS (SELECT @VAR = name .....
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

iarp
Starting Member

2 Posts

Posted - 09/06/2012 :  19:11:01  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 09/06/2012 :  19:39:54  Show Profile  Reply with Quote
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 - 09/06/2012 :  19:48:06  Show Profile  Reply with Quote

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


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.

Edited by - iarp on 09/06/2012 19:49:11
Go to Top of Page

sunitabeck
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 09/07/2012 :  07:40:00  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.11 seconds. Powered By: Snitz Forums 2000