Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12

conversion of the varchar value overflowed an int column Expand / Collapse
Author
Message
Posted Tuesday, May 7, 2013 7:09 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Thursday, July 10, 2014 11:20 AM
Points: 1,888, Visits: 2,323
ChrisM@Work (5/7/2013)
I'm sure there's an easier (and safer) way of doing this.

ALTER PROCEDURE [dbo].[BS_GetSTNValue] 
--DECLARE
@pFromStore int = 1001,
@pToStore int = 1004,
@pSTN VARCHAR(20) = 0 output
AS
--BEGIN

DECLARE
@count INT = 0,
@AWBNo VARCHAR(10) = 0,
@checkSTN varchar(10)

DECLARE @oldSTN VARCHAR(10)


------------------------------------------------------------------
-- If there isn't a row in GV_STNDetails
-- matching on @pFromStore and @pToStore
-- with a blank AWBNo (NULL or 0) then
-- create one, inserting only @pFromStore and @pToStore
IF NOT EXISTS (
SELECT 1
FROM GV_STNDetails gs
WHERE gs.From_StoreCode = @pFromStore
AND gs.TO_StoreCode = @pToStore
AND ISNULL(gs.AWBNo,0) = 0
)
BEGIN
INSERT INTO GV_STNDetails (From_StoreCode, TO_StoreCode)
VALUES (@pFromStore,@pToStore)
END

-------------------------------------------------------------------
-- Get a random STNNO from GV_STNDetails
-- matching on @pFromStore and @pToStore
SET @checkSTN = (
SELECT TOP 1 ISNULL(gs.STNNO, 0)
FROM GV_STNDetails gs
WHERE gs.From_StoreCode = @pFromStore
AND gs.TO_StoreCode = @pToStore
)
PRINT @checkSTN

-------------------------------------------------------------------
-- Get a random AWBNo from GV_STNDetails
-- matching on @pFromStore and @pToStore
SET @AWBNo = (
SELECT TOP 1 ISNULL(gs.AWBNo, 0)
FROM GV_STNDetails gs
WHERE gs.From_StoreCode = @pFromStore
AND gs.TO_StoreCode = @pToStore
)
PRINT @AWBNo

-------------------------------------------------------------------
-- Get the CountValue from GV_STNCountValues for @pFromStore
SET @count = (
SELECT gsv.CountValue
FROM GV_STNCountValues gsv
WHERE gsv.StoreCode = @pFromStore
)

IF @AWBNo = 0
BEGIN
--IF CAST(@checkSTN as BIGINT) = 0
IF @checkSTN = 0
BEGIN
SET @pSTN = CONVERT(VARCHAR, REPLACE(STR(@pFromStore, 4), ' ', 0))
+ CONVERT(VARCHAR, REPLACE(STR(@count + 1, 6), ' ', '0'))
PRINT 'When STN & AWB is 0 ' + @pSTN

UPDATE GV_STNDetails
SET STNNO = @pSTN
WHERE From_StoreCode = @pFromStore
AND TO_StoreCode = @pToStore
AND ISNULL(AWBNo,0) = 0

UPDATE GV_STNCountValues
SET CountValue = CountValue + 1
WHERE StoreCode = @pFromStore
END
ELSE
BEGIN
-- Get a random STNNO from GV_STNDetails
-- matching on @pFromStore and @pToStore
-- where AWBNo is blank (NULL or 0)
SET @pSTN = (
SELECT TOP 1 gs.STNNO
FROM GV_STNDetails gs
WHERE gs.From_StoreCode = @pFromStore
AND gs.TO_StoreCode = @pToStore
AND ISNULL(AWBNo,0) = 0)

-- assign the random STNNO to all rows in GV_STNDetails
-- matching on @pFromStore and @pToStore
-- where AWBNo is blank (NULL or 0)
UPDATE GV_STNDetails
SET STNNO = @pSTN
WHERE From_StoreCode = @pFromStore
AND TO_StoreCode = @pToStore
AND ISNULL(AWBNo,0) = 0

PRINT 'same ' + @pstn
END
END
ELSE
BEGIN
-- Get a random STNNO from GV_STNDetails
-- matching on @pFromStore and @pToStore
-- where AWBNo has a value (not NULL or 0)
SET @oldSTN = (
SELECT TOP 1 gs.STNNO
FROM GV_STNDetails gs
WHERE gs.From_StoreCode = @pFromStore
AND gs.TO_StoreCode = @pToStore
AND ISNULL(gs.AWBNo,0) <> 0
)

SET @pSTN = CONVERT(VARCHAR, REPLACE(STR(@pFromStore, 4), ' ', 0))
+ CONVERT(VARCHAR, REPLACE(STR(RIGHT(@oldSTN, 6) + 1,6),' ',0))
PRINT @pSTN

-- assign the random STNNO to all rows in GV_STNDetails
-- matching on @pFromStore and @pToStore
-- where AWBNo has a value (not NULL or 0)
UPDATE GV_STNDetails
SET STNNO = @pSTN
WHERE From_StoreCode = @pFromStore
AND TO_StoreCode = @pToStore
AND ISNULL(AWBNo,0) = 0

PRINT 'awb ' + @pSTN
END
RETURN @pSTN

--END

If you can correct the TOP(n) queries with ORDER BY, or better still MIN or MAX, it will clarify your intent. That in turn would make it much easier to rewrite this lot. It's well worth it.

Thanks Chris,
But the STN no is generation is based on FromStoreCode not random numbers....



_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Post #1450119
Posted Tuesday, May 7, 2013 7:14 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 7:58 AM
Points: 7,106, Visits: 13,459
kapil_kk (5/7/2013)
ChrisM@Work (5/7/2013)
I'm sure there's an easier (and safer) way of doing this.

ALTER PROCEDURE [dbo].[BS_GetSTNValue] 
--DECLARE
@pFromStore int = 1001,
@pToStore int = 1004,
@pSTN VARCHAR(20) = 0 output
AS
--BEGIN

DECLARE
@count INT = 0,
@AWBNo VARCHAR(10) = 0,
@checkSTN varchar(10)

DECLARE @oldSTN VARCHAR(10)


------------------------------------------------------------------
-- If there isn't a row in GV_STNDetails
-- matching on @pFromStore and @pToStore
-- with a blank AWBNo (NULL or 0) then
-- create one, inserting only @pFromStore and @pToStore
IF NOT EXISTS (
SELECT 1
FROM GV_STNDetails gs
WHERE gs.From_StoreCode = @pFromStore
AND gs.TO_StoreCode = @pToStore
AND ISNULL(gs.AWBNo,0) = 0
)
BEGIN
INSERT INTO GV_STNDetails (From_StoreCode, TO_StoreCode)
VALUES (@pFromStore,@pToStore)
END

-------------------------------------------------------------------
-- Get a random STNNO from GV_STNDetails
-- matching on @pFromStore and @pToStore
SET @checkSTN = (
SELECT TOP 1 ISNULL(gs.STNNO, 0)
FROM GV_STNDetails gs
WHERE gs.From_StoreCode = @pFromStore
AND gs.TO_StoreCode = @pToStore
)
PRINT @checkSTN

-------------------------------------------------------------------
-- Get a random AWBNo from GV_STNDetails
-- matching on @pFromStore and @pToStore
SET @AWBNo = (
SELECT TOP 1 ISNULL(gs.AWBNo, 0)
FROM GV_STNDetails gs
WHERE gs.From_StoreCode = @pFromStore
AND gs.TO_StoreCode = @pToStore
)
PRINT @AWBNo

-------------------------------------------------------------------
-- Get the CountValue from GV_STNCountValues for @pFromStore
SET @count = (
SELECT gsv.CountValue
FROM GV_STNCountValues gsv
WHERE gsv.StoreCode = @pFromStore
)

IF @AWBNo = 0
BEGIN
--IF CAST(@checkSTN as BIGINT) = 0
IF @checkSTN = 0
BEGIN
SET @pSTN = CONVERT(VARCHAR, REPLACE(STR(@pFromStore, 4), ' ', 0))
+ CONVERT(VARCHAR, REPLACE(STR(@count + 1, 6), ' ', '0'))
PRINT 'When STN & AWB is 0 ' + @pSTN

UPDATE GV_STNDetails
SET STNNO = @pSTN
WHERE From_StoreCode = @pFromStore
AND TO_StoreCode = @pToStore
AND ISNULL(AWBNo,0) = 0

UPDATE GV_STNCountValues
SET CountValue = CountValue + 1
WHERE StoreCode = @pFromStore
END
ELSE
BEGIN
-- Get a random STNNO from GV_STNDetails
-- matching on @pFromStore and @pToStore
-- where AWBNo is blank (NULL or 0)
SET @pSTN = (
SELECT TOP 1 gs.STNNO
FROM GV_STNDetails gs
WHERE gs.From_StoreCode = @pFromStore
AND gs.TO_StoreCode = @pToStore
AND ISNULL(AWBNo,0) = 0)

-- assign the random STNNO to all rows in GV_STNDetails
-- matching on @pFromStore and @pToStore
-- where AWBNo is blank (NULL or 0)
UPDATE GV_STNDetails
SET STNNO = @pSTN
WHERE From_StoreCode = @pFromStore
AND TO_StoreCode = @pToStore
AND ISNULL(AWBNo,0) = 0

PRINT 'same ' + @pstn
END
END
ELSE
BEGIN
-- Get a random STNNO from GV_STNDetails
-- matching on @pFromStore and @pToStore
-- where AWBNo has a value (not NULL or 0)
SET @oldSTN = (
SELECT TOP 1 gs.STNNO
FROM GV_STNDetails gs
WHERE gs.From_StoreCode = @pFromStore
AND gs.TO_StoreCode = @pToStore
AND ISNULL(gs.AWBNo,0) <> 0
)

SET @pSTN = CONVERT(VARCHAR, REPLACE(STR(@pFromStore, 4), ' ', 0))
+ CONVERT(VARCHAR, REPLACE(STR(RIGHT(@oldSTN, 6) + 1,6),' ',0))
PRINT @pSTN

-- assign the random STNNO to all rows in GV_STNDetails
-- matching on @pFromStore and @pToStore
-- where AWBNo has a value (not NULL or 0)
UPDATE GV_STNDetails
SET STNNO = @pSTN
WHERE From_StoreCode = @pFromStore
AND TO_StoreCode = @pToStore
AND ISNULL(AWBNo,0) = 0

PRINT 'awb ' + @pSTN
END
RETURN @pSTN

--END

If you can correct the TOP(n) queries with ORDER BY, or better still MIN or MAX, it will clarify your intent. That in turn would make it much easier to rewrite this lot. It's well worth it.

Thanks Chris,
But the STN no is generation is based on FromStoreCode not random numbers....


As long as you are using TOP without ORDER BY, you could get any FromStoreCode which is within scope of the WHERE clause. That's what I mean by random.

Use MAX() or MIN(), or use TOP with ORDER BY.


“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #1450123
Posted Tuesday, May 7, 2013 11:53 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Thursday, July 10, 2014 11:20 AM
Points: 1,888, Visits: 2,323
ChrisM@Work (5/7/2013)
kapil_kk (5/7/2013)
ChrisM@Work (5/7/2013)
I'm sure there's an easier (and safer) way of doing this.

ALTER PROCEDURE [dbo].[BS_GetSTNValue] 
--DECLARE
@pFromStore int = 1001,
@pToStore int = 1004,
@pSTN VARCHAR(20) = 0 output
AS
--BEGIN

DECLARE
@count INT = 0,
@AWBNo VARCHAR(10) = 0,
@checkSTN varchar(10)

DECLARE @oldSTN VARCHAR(10)


------------------------------------------------------------------
-- If there isn't a row in GV_STNDetails
-- matching on @pFromStore and @pToStore
-- with a blank AWBNo (NULL or 0) then
-- create one, inserting only @pFromStore and @pToStore
IF NOT EXISTS (
SELECT 1
FROM GV_STNDetails gs
WHERE gs.From_StoreCode = @pFromStore
AND gs.TO_StoreCode = @pToStore
AND ISNULL(gs.AWBNo,0) = 0
)
BEGIN
INSERT INTO GV_STNDetails (From_StoreCode, TO_StoreCode)
VALUES (@pFromStore,@pToStore)
END

-------------------------------------------------------------------
-- Get a random STNNO from GV_STNDetails
-- matching on @pFromStore and @pToStore
SET @checkSTN = (
SELECT TOP 1 ISNULL(gs.STNNO, 0)
FROM GV_STNDetails gs
WHERE gs.From_StoreCode = @pFromStore
AND gs.TO_StoreCode = @pToStore
)
PRINT @checkSTN

-------------------------------------------------------------------
-- Get a random AWBNo from GV_STNDetails
-- matching on @pFromStore and @pToStore
SET @AWBNo = (
SELECT TOP 1 ISNULL(gs.AWBNo, 0)
FROM GV_STNDetails gs
WHERE gs.From_StoreCode = @pFromStore
AND gs.TO_StoreCode = @pToStore
)
PRINT @AWBNo

-------------------------------------------------------------------
-- Get the CountValue from GV_STNCountValues for @pFromStore
SET @count = (
SELECT gsv.CountValue
FROM GV_STNCountValues gsv
WHERE gsv.StoreCode = @pFromStore
)

IF @AWBNo = 0
BEGIN
--IF CAST(@checkSTN as BIGINT) = 0
IF @checkSTN = 0
BEGIN
SET @pSTN = CONVERT(VARCHAR, REPLACE(STR(@pFromStore, 4), ' ', 0))
+ CONVERT(VARCHAR, REPLACE(STR(@count + 1, 6), ' ', '0'))
PRINT 'When STN & AWB is 0 ' + @pSTN

UPDATE GV_STNDetails
SET STNNO = @pSTN
WHERE From_StoreCode = @pFromStore
AND TO_StoreCode = @pToStore
AND ISNULL(AWBNo,0) = 0

UPDATE GV_STNCountValues
SET CountValue = CountValue + 1
WHERE StoreCode = @pFromStore
END
ELSE
BEGIN
-- Get a random STNNO from GV_STNDetails
-- matching on @pFromStore and @pToStore
-- where AWBNo is blank (NULL or 0)
SET @pSTN = (
SELECT TOP 1 gs.STNNO
FROM GV_STNDetails gs
WHERE gs.From_StoreCode = @pFromStore
AND gs.TO_StoreCode = @pToStore
AND ISNULL(AWBNo,0) = 0)

-- assign the random STNNO to all rows in GV_STNDetails
-- matching on @pFromStore and @pToStore
-- where AWBNo is blank (NULL or 0)
UPDATE GV_STNDetails
SET STNNO = @pSTN
WHERE From_StoreCode = @pFromStore
AND TO_StoreCode = @pToStore
AND ISNULL(AWBNo,0) = 0

PRINT 'same ' + @pstn
END
END
ELSE
BEGIN
-- Get a random STNNO from GV_STNDetails
-- matching on @pFromStore and @pToStore
-- where AWBNo has a value (not NULL or 0)
SET @oldSTN = (
SELECT TOP 1 gs.STNNO
FROM GV_STNDetails gs
WHERE gs.From_StoreCode = @pFromStore
AND gs.TO_StoreCode = @pToStore
AND ISNULL(gs.AWBNo,0) <> 0
)

SET @pSTN = CONVERT(VARCHAR, REPLACE(STR(@pFromStore, 4), ' ', 0))
+ CONVERT(VARCHAR, REPLACE(STR(RIGHT(@oldSTN, 6) + 1,6),' ',0))
PRINT @pSTN

-- assign the random STNNO to all rows in GV_STNDetails
-- matching on @pFromStore and @pToStore
-- where AWBNo has a value (not NULL or 0)
UPDATE GV_STNDetails
SET STNNO = @pSTN
WHERE From_StoreCode = @pFromStore
AND TO_StoreCode = @pToStore
AND ISNULL(AWBNo,0) = 0

PRINT 'awb ' + @pSTN
END
RETURN @pSTN

--END

If you can correct the TOP(n) queries with ORDER BY, or better still MIN or MAX, it will clarify your intent. That in turn would make it much easier to rewrite this lot. It's well worth it.

Thanks Chris,
But the STN no is generation is based on FromStoreCode not random numbers....


As long as you are using TOP without ORDER BY, you could get any FromStoreCode which is within scope of the WHERE clause. That's what I mean by random.

Use MAX() or MIN(), or use TOP with ORDER BY.

Ok thanks I will do modification to my script....



_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Post #1450408
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse