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
 General SQL Server Forums
 New to SQL Server Programming
 Help on query

Author  Topic 

johnspartan
Starting Member

2 Posts

Posted - 2009-02-13 : 05:53:20
Hi, i have a table with two columns:

* TYPE | int
* MSG | nvarchar(MAX)

My objective was to clean rows which column MSG ends with 'connected.' in blocks of 1000 rows. My question is, why does this query (notice the missing (MAX) from the declaration of @CRITERIA):

DECLARE @CRITERIA nvarchar, @ROWCOUNT Int
SET @ROWCOUNT = 1000
SET @CRITERIA = '% connected.'
WHILE @ROWCOUNT > 0
BEGIN
DELETE TOP(@ROWCOUNT)
FROM dbo.Eventos
WHERE TYPE = 2 AND MSG LIKE @CRITERIA
SELECT @ROWCOUNT = @@rowcount
END

erases ALL messages from table with TYPE=2 and this one (with the (MAX)) really does what's expected:

DECLARE @CRITERIA nvarchar(MAX), @ROWCOUNT Int
SET @ROWCOUNT = 1000
SET @CRITERIA = '% connected.'
WHILE @ROWCOUNT > 0
BEGIN
DELETE TOP(@ROWCOUNT)
FROM dbo.Eventos
WHERE TYPE = 2 AND MSG LIKE @CRITERIA
SELECT @ROWCOUNT = @@rowcount
END

Thanks!

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-02-13 : 05:58:29
If you don't set the precision for a varchar/nvarchar variable, the precision is automatically set to 1.
DECLARE	@p1 NVARCHAR,
@p2 NVARCHAR(MAX)

SELECT @p1 = '% connected.',
@p2 = '% connected.'

SELECT @p1,
@p2

SELECT LEN(@p1),
LEN(@p2)



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

johnspartan
Starting Member

2 Posts

Posted - 2009-02-13 : 06:29:49
Thank you Peso.

quote:
Originally posted by Peso

If you don't set the precision for a varchar/nvarchar variable, the precision is automatically set to 1.
DECLARE	@p1 NVARCHAR,
@p2 NVARCHAR(MAX)

SELECT @p1 = '% connected.',
@p2 = '% connected.'

SELECT @p1,
@p2

SELECT LEN(@p1),
LEN(@p2)



E 12°55'05.63"
N 56°04'39.26"


Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-02-13 : 09:19:35
http://sqlblogcasts.com/blogs/madhivanan/archive/2007/12/04/column-length-and-data-length.aspx
Go to Top of Page
   

- Advertisement -