dbTalk Databases Forums  

cursor insert with like '%'

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss cursor insert with like '%' in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
yurps
 
Posts: n/a

Default cursor insert with like '%' - 05-05-2005 , 12:45 PM






Hi I have a weird problem I want to cursor thru the values in a
temporary table and use the values to do a select statement to insert
into another temporary table...This select statement uses a like clause

something like where....when I take off the insert still nothing comes
back from the select...when I hardcode values it works...I get
results...is there something wrong with appending a +'%' to a value
read from a cursor???

DECLARE @DEPT VARCHAR(65)
SET @DEPT = "00201,00203"

DECLARE @TB_ABSENCES TABLE(DeptOrEmpId VARCHAR(65))
DECLARE @TB_DEPT TABLE ( V_DEPARTMENT_CODE VARCHAR(128) )

INSERT INTO @TB_DEPT (V_DEPARTMENT_CODE)
SELECT V_DEPT FROM [ISIS].[dbo].[FU_GET_DEPTS_FROM_STRING](',', @DEPT)

DECLARE DEPTS CURSOR FAST_FORWARD FOR
SELECT V_DEPARTMENT_CODE+'%' FROM @TB_DEPT

OPEN DEPTS
FETCH NEXT FROM DEPTS INTO @DEPT_CODE

WHILE @@FETCH_STATUS = 0
BEGIN

--INSERT INTO @TB_ABSENCES TABLE
SELECT Code from TB_EMPLOYEE_DEPARTMENT T2
WHERE T2.V_HIERARCHY_CODE LIKE @DEPT_CODE + '%'

FETCH NEXT FROM DEPTS INTO @DEPT_CODE

END

CLOSE DEPTS
DEALLOCATE DEPTS


Reply With Quote
  #2  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: cursor insert with like '%' - 05-05-2005 , 04:22 PM






yurps (yurps (AT) yahoo (DOT) co.uk) writes:
Quote:
Hi I have a weird problem I want to cursor thru the values in a
temporary table and use the values to do a select statement to insert
into another temporary table...This select statement uses a like clause

something like where....when I take off the insert still nothing comes
back from the select...when I hardcode values it works...I get
results...is there something wrong with appending a +'%' to a value
read from a cursor???
I would guess that there are trailing spaces. Rewrite as:

DECLARE @DEPT VARCHAR(65)
SET @DEPT = "00201,00203"

DECLARE @TB_ABSENCES TABLE(DeptOrEmpId VARCHAR(65))

INSERT INTO @TB_ABSENCES TABLE
SELECT Code
from TB_EMPLOYEE_DEPARTMENT T2
JOIN [ISIS].[dbo].[FU_GET_DEPTS_FROM_STRING](',', @DEPT) D
ON T2.V_HIERARCHY_CODE LIKE rtrim(V_DEPT) + '%'

Yeah, that's right. No cursor. There is no need for it, and it could
be costly in terms of performance.


--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp


Reply With Quote
  #3  
Old   
yurps
 
Posts: n/a

Default Re: cursor insert with like '%' - 05-06-2005 , 03:57 AM



Oh gosh !! a couple hours wasted on something that I didn't need
to....

thanks!


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.