473,385 Members | 1,597 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

cursor insert with like '%'

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

Jul 23 '05 #1
2 1435
yurps (yu***@yahoo.co.uk) writes:
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, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #2
Oh gosh !! a couple hours wasted on something that I didn't need
to....

thanks!

Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Michel Combe | last post by:
Hi all, I'm writing a program that will read an ASCII file periodically and update several tables in a MySQL database. My question is "Can I use the same cursor for several SQL requests (SELECT...
3
by: Sean | last post by:
Hi all I have a bit of a dilema that I am hoping some of you smart dudes might be able to help me with. 1. I have a table with about 50 million records in it and quite a few columns. 2. I...
3
by: DarthMacgyver | last post by:
Hello, I recently wrote a survey application. Each question is very similar. The first questions gives me a problem when there are multiple people taking the survey (The Database connection...
10
by: Neil | last post by:
I need to get two values from a complex SQL statement which returns a single record and use those two values to update a single record in a table. In order to assign those two values to variables...
3
by: Developer | last post by:
I use the RichTextBox and sometimes put it into overwrite mode. I'd like the cursor to show whether its in overwrite or insert mode. I often use Cursor.Current = Cursors.WaitCursor and...
10
by: technocrat | last post by:
Hi, I am trying to declare and cursor and thn load from that cursor into another table. Since I have almost 4 million records, I cant do it without the cursor which reduces the time by almost...
8
by: Patti | last post by:
I am new to SQL and have created a stored procedure for a .net web application. Unfortunately I had to use a cursor in the stored procedure, so it is taking several minutes to execute because it...
4
by: CK | last post by:
Good Morning All, Can use use a variable for the FOR clause in a cursor? Example I have DECLARE @a varchar(50), @b varchar(50), @c varchar(50) DECLARE @sql varchar(255) DECLARE @x...
36
by: CK | last post by:
How do I write a set based query? I have a groupSets table with fields setId, idField, datasource, nameField, prefix, active Data: 1,someIDfield, someTable, someField, pre1, 1 2,someotherIDfield,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.