473,406 Members | 2,208 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,406 software developers and data experts.

Set Approach instead of Cursor

Hi,

I am trying a Set Approach instead of Using of Cursor (which works).

I am attaching the SQL to create tables and the my Procedure, and a
piece of code to execute the Procedure.

I would like the Procedure ReplaceTags to work with 'a' the same as
with 'C'.

Thanks in advance.

Hareesh

/*****************************/
/* Create Tables */

IF EXISTS (SELECT * FROM dbo.sysobjects WHERE ID =
OBJECT_ID(N'GlobalTags') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
DROP TABLE GlobalTags
GO

CREATE TABLE GlobalTags
(
Project VARCHAR(50) NULL,
TagName VARCHAR(50) NULL,
[Value] VARCHAR(50) NULL
)
GO

IF EXISTS (SELECT * FROM dbo.sysobjects WHERE ID =
OBJECT_ID(N'ProductDetails') and OBJECTPROPERTY(id, N'IsUserTable') =
1)
DROP TABLE ProductDetails
GO

CREATE TABLE ProductDetails
(
Project VARCHAR(50) NULL,
KeyName VARCHAR(50) NULL,
[Value] VARCHAR(50) NULL
)
GO
/*********************************/
/* Populate Tables */

TRUNCATE TABLE GlobalTags

INSERT INTO GlobalTags (Project, TagName, Value)
VALUES('ProjectName', 'FirstName', 'John')
INSERT INTO GlobalTags (Project, TagName, Value)
VALUES('ProjectName', 'LastName', 'Doe')
INSERT INTO GlobalTags (Project, TagName, Value)
VALUES('ProjectName', 'PhoneNo', '5248')
INSERT INTO GlobalTags (Project, TagName, Value)
VALUES('ProjectName', 'ZIPCode', '55555')

TRUNCATE TABLE ProductDetails

INSERT INTO ProductDetails (Project, KeyName, Value)
VALUES('ProjectName', 'FirstName', '%FirstName%')
INSERT INTO ProductDetails (Project, KeyName, Value)
VALUES('ProjectName', 'LastName', '%LastName%')
INSERT INTO ProductDetails (Project, KeyName, Value)
VALUES('ProjectName', 'PhoneNo', '%PhoneNo%')
INSERT INTO ProductDetails (Project, KeyName, Value)
VALUES('ProjectName', 'ZIPCode', '%ZIPCode%')

/****************************/
/* Procedure */

IF EXISTS (SELECT * FROM sysobjects WHERE name = 'ReplaceTags')
DROP PROCEDURE ReplaceTags
GO

CREATE PROCEDURE ReplaceTags
(
@aProjectName VARCHAR(50),
@aProcessType CHAR(1)
)
AS
BEGIN

DECLARE @TagName VARCHAR(50)
DECLARE @Value VARCHAR(50)

IF @aProcessType = 'C'
BEGIN
DECLARE REPLACE_CURSOR CURSOR FAST_FORWARD READ_ONLY FOR
SELECT TagName, Value FROM GlobalTags
WHERE Project = @aProjectName

OPEN REPLACE_CURSOR
FETCH NEXT FROM REPLACE_CURSOR INTO @TagName, @Value

WHILE (@@FETCH_STATUS = 0)
BEGIN

UPDATE ProductDetails
SET
Value =
CASE WHEN CHARINDEX('%' + @TagName + '%', Value, 1) > 0
THEN
REPLACE(Value, '%' + @TagName + '%', @Value)
ELSE
Value
END
WHERE Project = @aProjectName

FETCH NEXT FROM REPLACE_CURSOR INTO @TagName, @Value
END

CLOSE REPLACE_CURSOR
DEALLOCATE REPLACE_CURSOR

END
ELSE
BEGIN
UPDATE ProductDetails
SET
Value =
CASE WHEN CHARINDEX('%' + GlobalTags.TagName + '%',
ProductDetails.Value, 1) > 0 THEN
REPLACE(ProductDetails.Value, '%' +
GlobalTags.TagName + '%', GlobalTags.Value)
ELSE
ProductDetails.Value
END
FROM ProductDetails INNER JOIN GlobalTags
ON (ProductDetails.Project = GlobalTags.Project)
WHERE ProductDetails.Project = @aProjectName

END

END

/***********************************/
/* Run Procedure */

EXECUTE ReplaceTags 'ProjectName', 'a'
-- EXECUTE ReplaceTags 'ProjectName', 'C'

SELECT * FROM GlobalTags
SELECT * FROM ProductDetails

/* End*/
/***********************************/
Jul 20 '05 #1
6 3504

"Hareesh" <Ha***********@Rapistan.com> wrote in message
news:e5*************************@posting.google.co m...
Hi,

I am trying a Set Approach instead of Using of Cursor (which works).

I am attaching the SQL to create tables and the my Procedure, and a
piece of code to execute the Procedure.

I would like the Procedure ReplaceTags to work with 'a' the same as
with 'C'.

Thanks in advance.

Hareesh


<snip>

Thanks for the sample code, but it would be useful to know the keys of the
tables. Assuming that (Project, TagName) and (Project, KeyName) are the
keys, then this is one possibility:

update ProductDetails
set Value = gt.Value
from ProductDetails pd
join GlobalTags gt
on gt.Project = pd.Project
and gt.TagName = pd.KeyName
where charindex('%', pd.Value) > 0
and pd.Project = 'ProjectName'

Simon
Jul 20 '05 #2
Hi,

There has been a misunderstanding.
Let me clarify the problem.

The tables are as they are, with no keys.
There is no relationship between GlobalTags.TagName and
ProductDetails.KeyName
Basically the TagName from GlobalTags will appear in the Value field
of ProductDetails, that particular occurence of the TagName has to be
replaced with the value from the GlobalTags.

Let me add two more records to make the problem more clear.

INSERT INTO ProductDetails (Project, KeyName, Value)
VALUES('ProjectName', 'Name', 'xxx%FirstName%')
INSERT INTO ProductDetails (Project, KeyName, Value)
VALUES('ProjectName', 'Name', 'yyy%LastName%')

Hareesh
Jul 20 '05 #3
>> The tables are as they are, with no keys. <<

Then **by definition** these are not tables. And you cannot process
them in an RDBMS environment. Using SQL for sequential file
processing is a waste of resources.
There is no relationship between GlobalTags.TagName and ProductDetails.KeyName <<

A relational database is called "relational" because there ARE
relationships!
Basically the TagName from GlobalTags will appear in the Value

field [sic]
of ProductDetails, that particular occurence of the TagName has to be
replaced with the value from the GlobalTags. Let me add two more
records [sic] to make the problem more clear. <<

Rows are not records; fields are not columns; tables are not files;
there is no sequential access or ordering in an RDBMS, so "first",
"next" and "last" are totally meaningless.

You need to read a book on SQL and RDBMS; everything you have done is
fundametnally wrong.
Jul 20 '05 #4
Joe,
You need to read a book on SQL and RDBMS; everything you have done is
fundametnally wrong.


I am aware of SQL and RDBMS, thank you.
I do not need unsolicited advice.
Stick to the problem.

I do agree that this is file processing than relational database processing.
The problem is because of what I inherited.

Hareesh
Jul 20 '05 #5
>> I do not need unsolicited advice. <<

Then why are you posting to newsgroups?
Stick to the problem. <<
The problem IS an unusable design
I do agree that this is file processing than relational database processing. <<

Then do it in a language that supports file processing, a front end host
application.

I have kludged this kind of problem before; formatting data onthe server
side in violation of basic C/S design.

It eats resources on the server side and the code becomes very complex
when NULLs are taken into consideration. Changes in the application,
such as the width of the display lines, cascade back to the server side.

Based on my experience, this is not the way to solve the problem.
The problem is because of what I inherited. <<


Fire the person who did this, and re-write all his code.
Start over; do it right. I have charged people thousands of dollars for
exactly that advice :)

--CELKO--
===========================
Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, datatypes, etc. in your
schema are.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #6
> Then why are you posting to newsgroups?
To find if there was some way to perform in a
Relational Database way which I missed.
Fire the person who did this, and re-write all his code.
Start over; do it right.

I wish I could do that. Believe me.
It is too late.
It is very difficult to educate people without an
understanding of Relational Database, more difficult
if they are in a higher position and have been in
the company longer and had already impressed
the more ignorant higher ups.
Jul 20 '05 #7

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

Similar topics

4
by: Dan | last post by:
I am loading data from table A into table B. Certain columns in B have check constraints. I'd like for any rows from A, which violate constraints, to be inserted into a third table, C. When the...
2
by: dave | last post by:
In my form Ive got a SaveData() routine that saves changes to a DB. When I encounter an exception in the save operations, I am having trouble chaning the cursor back to the default cursor, it just...
10
by: Just Me | last post by:
Does Me.Cursor.Current=Cursors.WaitCursor set the current property of Me.Cursor to Cursors.WaitCursor And Me.Cursor.Current=Cursors.Default set the Me.Current property to something (default)...
15
by: Rob Meade | last post by:
Hi all, I have a databse which I'm pulling the data from for my ASP page. I have 4 tables, Course, Feature, Objective, and PreRequisite. The last three all contain a course product code and a...
5
by: AA Arens | last post by:
I want to select a value in a long list of the combo box. How to have this lstarted with the upper most value instaed of the last choosen value. (By the way, I approach the box with...
0
by: anonieko | last post by:
This approach I found very efficient and FAST when compared to the rowcount, or Subquery Approaches. This is before the advent of a ranking function from DB such as ROW_NUMBER() in SQL Server...
0
by: nidhidshah | last post by:
I am using stored procedures (in IBM DB2) with Crystal Reports XI to create reports. When I run the report from Crystal Reports, it runs fine for the first time. But second time onwards I get an...
0
debasisdas
by: debasisdas | last post by:
RESTRICTIONS ON CURSOR VARIABLES ================================= Currently, cursor variables are subject to the following restrictions: Cannot declare cursor variables in a package spec. ...
20
by: mike3 | last post by:
Hi. (Xposted to both comp.lang.c++ and comp.programming since I've got questions related to both C++ language and general programming) I've got the following C++ code. The first routine runs in...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.