473,804 Members | 2,225 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Cursor and Procedure

Hello:

I want to find that the ntext column data string have more than 2000
characters. I need to truncate those strings to the segments with 200
character, then put those segments along with their table_name and
column_name to another table. Maybe need to use cursor? If so, how to
use it?

Your help is highly appreciated.

S
Jun 27 '08 #1
4 2169
Snow (cs*******@gmai l.com) writes:
I want to find that the ntext column data string have more than 2000
characters. I need to truncate those strings to the segments with 200
character, then put those segments along with their table_name and
column_name to another table. Maybe need to use cursor? If so, how to
use it?

Your help is highly appreciated.
It's difficult to work with ntext columns more than one a time, so it
sounds like you need to use a cursor. But your description is far too
terse for me to want to give an example. Could you post:

1) CREATE TABLE statements for your table(s).
2) INSERT statements with sample data.
3) The desired result given the sample.

Since it's unpractical to post strings with 2000 characters, you
could pretent that the limit is 50 characters or whatever when you
compose the example.
--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Jun 27 '08 #2
On May 18, 5:07*pm, Erland Sommarskog <esq...@sommars kog.sewrote:
Snow (cs180y...@gmai l.com) writes:
I want to find that the ntext column data string have more than 2000
characters. I need to truncate those strings to the segments with 200
character, then put those segments along with their table_name and
column_name to another table. Maybe need to use cursor? If so, how to
use it?
Your help is highly appreciated.

It's difficult to work with ntext columns more than one a time, so it
sounds like you need to use a cursor. But your description is far too
terse for me to want to give an example. Could you post:

1) *CREATE TABLE statements for your table(s).
2) *INSERT statements with sample data.
3) *The desired result given the sample.

Since it's unpractical to post strings with 2000 characters, you
could pretent that the limit is 50 characters or whatever when you
compose the example.

--
Erland Sommarskog, SQL Server MVP, esq...@sommarsk og.se

Books Online for SQL Server 2005 athttp://www.microsoft.c om/technet/prodtechnol/sql/2005/downloads/books...
Books Online for SQL Server 2000 athttp://www.microsoft.c om/sql/prodinfo/previousversion s/books.mspx


Hello:

Thanks for the reply. Actually problem is ntext hold 4000 char. I
need to split ntext data into the segnments with 4000 char, and then
put those segnments to the another table. For example: The ntext data
string has 12000 char. It will be split to 3 segments. The another
table has the char field: long_seg. The column has segnment1(4000
char), segnment2(4000 char), segnment 3(4000 char) which comes from
the splitting ntext data sting with 12000 char. Another field:
segment_nbr holds segment number : 1, 2, and 3.

Here is the table to get the splitting segments.

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].
[tbl_longdata]') and OBJECTPROPERTY( id, N'IsUserTable') = 1)
drop table [dbo].[tbl_longdata]
GO

CREATE TABLE [dbo].[tbl_longdata] (
[id] [int] IDENTITY (1, 1) NOT NULL ,
[table_name] [char] (50) COLLATE SQL_Latin1_Gene ral_CP1_CI_AS NULL ,
[col_name] [char] (50) COLLATE SQL_Latin1_Gene ral_CP1_CI_AS NULL ,
[tableId] [int] NULL ,
[segment_nbr] [int] NULL ,
[long_seg] [nvarchar] (4000) COLLATE SQL_Latin1_Gene ral_CP1_CI_AS
NULL
) ON [PRIMARY]
GO

Here is the simple cursor test one table: tbl_deptTri.
DECLARE @id_cur int
DECLARE @deptTriDes ntext
Declare @segment_nbr_cu r int
DECLARE @segment_nbr_ne w INT
DECLARE @deptTriDes_cur ntext
DECLARE @stringpos INT

DECLARE TableCursor CURSOR FOR
SELECT [id], [deptTriDes]
FROM tbl_deptTri

OPEN TableCursor

FETCH NEXT FROM TableCursor INTO @id_cur, @deptTriDes_cur

WHILE @@FETCH_STATUS = 0
BEGIN
SET @segment_nbr_ne w = 0
SET @segment_nbr_cu r = CEILING(DATALEN GTH(@deptTriDes _cur)/8000)
WHILE @segment_nbr_ne w <= @segment_nbr_cu r
BEGIN
SET @segment_nbr_ne w = @segment_nbr_ne w + 1
SET @stringpos = (@segment_nbr_n ew - 1)*4000 + 1
INSERT INTO tbl_LongData ([table_name],[col_name],[tableID],
[segment_nbr],[long_seg])
VALUES (
'tbl_deptTri',
'deptTriDes',
@id_cur,
@segment_nbr_ne w,
SUBSTRING(@dept TriDes_cur,@str ingpos,4000)
)
END
FETCH NEXT FROM TableCursor INTO @id_cur, @deptTriDes_cur
END
CLOSE TableCursor
DEALLOCATE TableCursor

However, I got the error: The text, ntext, and image data types are
invalid for local variables.

what am I missing here?
Thanks
Jun 27 '08 #3
Snow (cs*******@gmai l.com) writes:
However, I got the error: The text, ntext, and image data types are
invalid for local variables.

what am I missing here?
Exactly what it says. I will have to look into this tonight to see if
I can think of something. But the in my while, maybe you can answer one
question that I forgot: which version of SQL Server are you using?
--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Jun 27 '08 #4
Snow (cs*******@gmai l.com) writes:
However, I got the error: The text, ntext, and image data types are
invalid for local variables.
OK, assuming that you are stuck on SQL 2000 and cannot move to the new
MAX data types, the solution would be always read the column from the
table, as in this example (which is untested, as I did not have the
source table, nor any test data):

DECLARE @id_cur int
DECLARE @deptTriDes ntext
DECLARE @segment_nbr_ne w INT
DECLARE @stringpos INT

DECLARE TableCursor CURSOR FOR
SELECT [id], [deptTriDes]
FROM tbl_deptTri

OPEN TableCursor

FETCH NEXT FROM TableCursor INTO @id_cur, @deptTriDes_cur

WHILE @@FETCH_STATUS = 0
BEGIN
SET @segment_nbr_ne w = 0

SELECT @segment_nbr_cu r = CEILING(DATALEN GTH(deptTriDes)/8000)
FROM tbl_deptTri
WHERE id = @id_cur

WHILE @segment_nbr_ne w <= @segment_nbr_cu r
BEGIN
SET @segment_nbr_ne w = @segment_nbr_ne w + 1
SET @stringpos = (@segment_nbr_n ew - 1)*4000 + 1
INSERT INTO tbl_LongData ([table_name],[col_name],[tableID],
[segment_nbr],[long_seg])
SELECT
'tbl_deptTri',
'deptTriDes',
@id_cur,
@segment_nbr_ne w,
SUBSTRING(deptT riDes, @stringpos, 4000)
FROM tbl_deptTri
WHERE id = @id_cur
END
FETCH NEXT FROM TableCursor INTO @id_cur
END
CLOSE TableCursor
DEALLOCATE TableCursor

--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Jun 27 '08 #5

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

Similar topics

7
2231
by: Philip Mette | last post by:
Does anyone have any good references they could recommend on Cursor based SQL writing? I have to create SQL that can loop though records simular to VB loops and I have been told that this is the way to go. Any recommendations would be helpful.
15
3828
by: Philip Mette | last post by:
I am begginner at best so I hope someone that is better can help. I have a stored procedure that updates a view that I wrote using 2 cursors.(Kind of a Inner Loop) I wrote it this way Because I couldn't do it using reqular transact SQL. The problem is that this procedure is taking longer and longer to run. Up to 5 hours now! It is anaylizing about 30,000 records. I think partly because we add new records every month. The procedure...
3
6118
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 Timed out) I am using the Data Access Application Blocks as ASP.NET (using VB.NET) and SQL 2000. In there first question there can be up to 27 answers. So I figured instead of making 27 different trips to the database I woulc just concatenate my...
10
1701
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 and then use those variables in the UPDATE statement, I created a cursor and used Fetch Next.... Into. This way, I only have to call the complex SQL once instead of twice. This seems like the best way to go. However, I've always used cursors for...
2
9500
by: ansonee | last post by:
I wrote a stored procedure that contains some COMMIT logic, but keep runnign into an odd problem. As soon as the procedure gets to the COMMIT statement, the CURSOR closes. Here's the procedure: CREATE PROCEDURE AIM.UPDATEARCHIVERETRIEVALSTATUS ( ) SPECIFIC AIM.UPDATEARCRETSTS LANGUAGE SQL P1: BEGIN DECLARE RECORDCOUNT INTEGER;
8
14024
by: Yusuf INCEKARA | last post by:
I have a stored procedure : CREATE PROCEDURE STP_GETSTORELIST @RETCUR CURSOR VARYING OUTPUT AS set @RETCUR = CURSOR FORWARD_ONLY STATIC FOR SELECT ID,STORE_NAME FROM T_INF_STORE ORDER BY STORE_NAME OPEN @RETCUR
12
31010
by: Lucky | last post by:
Hi guys! i want to create one cursor in the t-sql. the problem is i want to use stored procedure instead of select command in cursor. can anyone tell me how can i use stored procedure's o/p to create cursor? i'm using sql 2000 and .net 2.0 thanks,
10
3263
by: 4.spam | last post by:
Hello. v8.2.7, windows xp. --- create procedure cursor_test() language sql dynamic result sets 1 begin declare c1 cursor
2
3882
by: BilalGhazi | last post by:
Hi All, I have this strange problem. I am user of two different database (both are same version 9i). I created a procedure and within this procedure i used a cursor to select the values, this is working very fine. When i copy this procedure in other database and try to run the procedure, the procedure runs successful, but the cursor does not populate the values, when i run the query written in the cursor, query returns the correct values....
1
1791
by: love2livedislife | last post by:
I have a requirement as follows: while executing the procedure for first time i'll open a cursor and read the first row from it and return the value through OUT parameter of procedure without closing the cursor... during the second time execution of the same procedure i need the cursor to be opened so that i can read the second row during second execution of procedure and so on..?? i will maintain another OUT parameter which says...
0
9715
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9595
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10603
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10356
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10099
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9176
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7643
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5536
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5675
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.