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 2005 Forums
 Transact-SQL (2005)
 query problem

Author  Topic 

awaisahmad435
Starting Member

11 Posts

Posted - 2011-06-01 : 06:11:02

i have created a stored procedure which runs Perfect in SQL Server 2008 but producing error in SQL Server 2005... The error is

only one expression can be specified in the select list when the subquery is not introduced with exists


the procedure is below. plz help me where i m wrong...



CREATE PROCEDURE Refresh_Lock

AS

DECLARE @AccountID VARCHAR(100)
DECLARE @getAccountID CURSOR
SET @getAccountID = CURSOR FOR
SELECT TABLENAME
FROM DBO.TMP
OPEN @getAccountID
FETCH NEXT
FROM @getAccountID INTO @AccountID
WHILE @@FETCH_STATUS = 0
BEGIN
PRINT @AccountID
EXEC('update ' + @AccountID + '
set flag = ''FREE9''
where SYSTEMID in
(select b.systemid
from sys.dm_exec_connections a
right outer join dbo.TMP b on b.ipaddress = a.client_net_address
where a.session_id is null
and b.tablename = ''' + @AccountID + ''')')
EXEC('delete from dbo.tmp
where SYSTEMID in
(select b.systemid
from sys.dm_exec_connections a
right outer join dbo.TMP b on b.ipaddress = a.client_net_address
where a.session_id is null
and b.tablename = ''' + @AccountID + ''')')

FETCH NEXT
FROM @getAccountID INTO @AccountID
END
CLOSE @getAccountID
DEALLOCATE @getAccountID

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2011-06-01 : 06:29:56
Which statement is giving you the error?
Check that the set cursor statement is valid in v2005.

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

awaisahmad435
Starting Member

11 Posts

Posted - 2011-06-01 : 08:47:25
the problem arises where i use IN clause. 2005 compiler says u cant use IN clause here. use Exists. but Exists clause doestnot solve my problem.....
but 2008 compiler doest'n raise any issue and run it perfectly...
Go to Top of Page

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2011-06-01 : 09:00:50
You can always use an exist to replace an in clause.
You have two statements with in clasues - do they both fail?
Have you tried executing the query rather than running it in the sp? Try it with just a select statement.

Could be something to do with sys.dm_exec_connections.

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2011-06-01 : 09:06:24
quote:
Originally posted by awaisahmad435


i have created a stored procedure which runs Perfect in SQL Server 2008 but producing error in SQL Server 2005... The error is

only one expression can be specified in the select list when the subquery is not introduced with exists


the procedure is below. plz help me where i m wrong...



CREATE PROCEDURE Refresh_Lock

AS

DECLARE @AccountID VARCHAR(100)
DECLARE @getAccountID CURSOR
SET @getAccountID = CURSOR FOR
SELECT TABLENAME
FROM DBO.TMP
OPEN @getAccountID
FETCH NEXT
FROM @getAccountID INTO @AccountID
WHILE @@FETCH_STATUS = 0
BEGIN
PRINT @AccountID
EXEC('update ' + @AccountID + '
set flag = ''FREE9''
where SYSTEMID in
(select b.systemid
from sys.dm_exec_connections a
right outer join dbo.TMP b on b.ipaddress = a.client_net_address
where a.session_id is null
and b.tablename = ''' + @AccountID + ''')')
EXEC('delete from dbo.tmp
where SYSTEMID in
(select b.systemid
from sys.dm_exec_connections a
right outer join dbo.TMP b on b.ipaddress = a.client_net_address
where a.session_id is null
and b.tablename = ''' + @AccountID + ''')')

FETCH NEXT
FROM @getAccountID INTO @AccountID
END
CLOSE @getAccountID
DEALLOCATE @getAccountID




No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

awaisahmad435
Starting Member

11 Posts

Posted - 2011-06-01 : 09:15:40
quote:
Originally posted by nigelrivett

You can always use an exist to replace an in clause.
You have two statements with in clasues - do they both fail?
Have you tried executing the query rather than running it in the sp? Try it with just a select statement.

Could be something to do with sys.dm_exec_connections.

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.



but EXIST clause update whole column. and i want to update where systemid matches with subquery.
in EXIST clause we cant mention columnname but with IN clause we can mention columnname .....

yes my both statements failed. and nothing wrong with sys.dm_exec_connections. im sure
Go to Top of Page

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2011-06-01 : 09:55:35
Neither in nor exists updates a column - they both filter the rows that get updated.
update mytbl
set ...
from mytblt t1
where t1.col in (select t2.col from tbl t2)

is the same as
update mytbl
set ...
from mytblt t1
where exists (select * from tbl t2 where t2.col = t1.col)

>> nothing wrong with sys.dm_exec_connections. im sure
CHeck the way it can be used. There might be nothing wrong with it but the implementation in the two versions might restrict the way it can be used. You don't know unless you check.

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

awaisahmad435
Starting Member

11 Posts

Posted - 2011-06-01 : 10:24:09
quote:
Originally posted by nigelrivett

Neither in nor exists updates a column - they both filter the rows that get updated.
update mytbl
set ...
from mytblt t1
where t1.col in (select t2.col from tbl t2)

is the same as
update mytbl
set ...
from mytblt t1
where exists (select * from tbl t2 where t2.col = t1.col)

>> nothing wrong with sys.dm_exec_connections. im sure
CHeck the way it can be used. There might be nothing wrong with it but the implementation in the two versions might restrict the way it can be used. You don't know unless you check.

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.


why u use * in 2nd query's subquery. Plz quide me that in wat way EXISTS works. i m still confused...
In 2nd query, after where clause EXISTS filters which of column of t1??? i mean to say with IN clause we specify specifically the column which will be filtered. but with EXISTS on the base of which column filter occurs
Go to Top of Page

awaisahmad435
Starting Member

11 Posts

Posted - 2011-06-01 : 11:19:34
can any one reconstruct my query with INNER JOIN...
I WANT TO ELIMINATE IN CLASS.... IS THAT POSSIBLE


update TBL_DEGREE_COURE_MASTER

set flag = 'FREE', flagtime = 'Application Crashed'

where SYSTEMID IN

(select b.systemid
from sys.dm_exec_connections a
right outer join dbo.tbl_locks b on b.ipaddress = a.client_net_address
where a.session_id is null
and b.table_name = 'TBL_DEGREE_COURE_MASTER')
Go to Top of Page

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2011-06-01 : 11:40:07
* just stands for a row - it normally returns all columns but the server recognises that this is an exists clause and just uses whatever indexes are available to run the query most efficiently.

To use a join

update TBL_DEGREE_COURE_MASTER
set flag = 'FREE', flagtime = 'Application Crashed'
from TBL_DEGREE_COURE_MASTER t
join
(select b.systemid
from sys.dm_exec_connections a
right outer join dbo.tbl_locks b on b.ipaddress = a.client_net_address
where a.session_id is null
and b.table_name = 'TBL_DEGREE_COURE_MASTER') t2
on t.SYSTEMID = t2.SYSTEMID



==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

awaisahmad435
Starting Member

11 Posts

Posted - 2011-06-01 : 12:35:55
quote:
Originally posted by nigelrivett

* just stands for a row - it normally returns all columns but the server recognises that this is an exists clause and just uses whatever indexes are available to run the query most efficiently.

To use a join

update TBL_DEGREE_COURE_MASTER
set flag = 'FREE', flagtime = 'Application Crashed'
from TBL_DEGREE_COURE_MASTER t
join
(select b.systemid
from sys.dm_exec_connections a
right outer join dbo.tbl_locks b on b.ipaddress = a.client_net_address
where a.session_id is null
and b.table_name = 'TBL_DEGREE_COURE_MASTER') t2
on t.SYSTEMID = t2.SYSTEMID



==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.




WOW WORKING PERFECT....THANKS A LOT DEAR.....

PLZ DO THE SAME WITH MY SECOND STATEMENT... THAT WAS


delete from dbo.tbl_locks

Where SYSTEMID IN

(select b.systemid
from sys.dm_exec_connections a
right outer join dbo.tbl_locks b on b.ipaddress = a.client_net_address
where a.session_id is null
and b.table_name = 'TBL_DEGREE_COURSE_MASTER')
Go to Top of Page

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2011-06-01 : 12:41:15
It's very similar

delete dbo.tbl_locks
from dbo.tbl_locks t
join
(select b.systemid
from sys.dm_exec_connections a
right outer join dbo.tbl_locks b on b.ipaddress = a.client_net_address
where a.session_id is null
and b.table_name = 'TBL_DEGREE_COURSE_MASTER') t2
on t.SYSTEMID = t2.SYSTEMID



==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

awaisahmad435
Starting Member

11 Posts

Posted - 2011-06-01 : 12:45:06
quote:
Originally posted by nigelrivett

It's very similar

delete dbo.tbl_locks
from dbo.tbl_locks t
join
(select b.systemid
from sys.dm_exec_connections a
right outer join dbo.tbl_locks b on b.ipaddress = a.client_net_address
where a.session_id is null
and b.table_name = 'TBL_DEGREE_COURSE_MASTER') t2
on t.SYSTEMID = t2.SYSTEMID



==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.




BUNDLE OF THANKS DEAR......
UR SO SWEET......
I HAVE RUN IT ON SQL SERVER 2008
WORKING PERFECT
TOMORROW I SHALL RUN IT ON SQL SERVER 2005....
THANKS AGAIN
Go to Top of Page
   

- Advertisement -