Two multi-value parameters in SSRS - sql

I have two multi-value parameters in my SQL: where (PayToTIN IN (#PayToTIN) or PCP_ProvId IN (#PCP_ProvId). I am trying to drop in multiple values in either or parameters however when I drop in a value in either parameter, SSRS treats the other parameter as ''. I am also unable to null out the either param because they are multi-value. Is there a workaround to run multiple values in either one or the other parameter?
Declare #PayToTIN as varchar(max) = '562660402' --'562660402'
Declare #PCP_ProvId as varchar(max) --= 'LASRL12550' --'LASRL12550'
declare #detailfields as varchar(max) = NULL
declare #PayToTIN_switch varchar(max) =
( select max(PayToTIN)
from LA_Temp.dbo.vMemberPCP
where cast(PayToTIN as varchar) in (#PayToTIN)
)
declare #PCPAssignmentID_switch varchar(max) =
( select max(PCP_ProvId)
from LA_Temp.dbo.vMemberPCP
where cast(PCP_ProvId as varchar) in (#PCP_ProvId)
)
declare #include_PayToTIN bit = case when 'Pay To Tin' in (#detailfields) then 1 else 0 end
declare #include_PCP_ProvId bit = case when 'PCP ProvID' in (#detailfields) then 1 else 0 end
select distinct
x.CarrierMemID
-- , MemberName
, MbrLastName =rtrim( e.LastName)
, MbrFirstName = rtrim(e.FirstName)
, MbrMI = rtrim(e.MiddleName)
, x.DOB
, x.Age
, x.Sex
, Address = e.addr1
, City = e.city
, State = e.state
, Zip = e.zip
, Parish = e.county
, MemPhone = e.phone
, PCPName
, PCP_NPI
, PCPProvID
, PCPEffDate
, Specialty
, ServiceLocation
, PayToProvider
, PayToTIN
, PayToProvID
, PCP_ProvId
, PCPAssignment
from LA_Temp.dbo.vMemberPCP x
inner join PlanReport_QNXT_LA.dbo.enrollkeys ek
on x.enrollid = ek.enrollid
inner join PlanReport_QNXT_LA.dbo.orgpolicy o
on o.orgpolicyid = ek.orgpolicyid
and right(rtrim(o.policynum),2) <> 'BH'
inner join PlanReport_QNXT_LA.dbo.member m
on ek.memid = m.memid
inner join PlanReport_QNXT_LA.dbo.entity e
on e.entid = m.entityid
--where PayToTIN = #PayToTIN --AMB 05/07/18
where (PayToTIN IN (#PayToTIN)
or '' = #PayToTIN_switch
)
or ( PCP_ProvId IN (#PCP_ProvId)
or '' = #PCPAssignmentID_switch
)

where (case when PayToTIN = '' then null else PayToTIN end in (#PayToTIN))
OR (case when PCP_ProvId = '' then null else PCP_ProvId end in (#PCP_ProvId))

Related

Procedure showing an error related to merge in sql and please verify that overall code is fine or not

Code is showing an error
Merge statements with a WHEN NOT MATCHED [BY TARGET] clause must target a hash distributed table
Also please verify that the overall syntax is fine or not?
MERGE IR_CREDITREQUEST_SPTESTING AS T
USING (
SELECT
TT.TaxYear
,TT.TaxPayerSSN
,TT.EngagementID
,TT.CreditRequestType
,TT.BorrowerID
,TT.NameSuffix
,TT.FirstName
,TT.MiddleName
,TT.LastName
,TT.SpouseName
,TT.SpouseSSN
,TT.PrintPositionType
,TT.MaritalStatusType
,TT.StreetAddress
,TT.City
,TT.State
,TT.PostalCode
,TT.Type
,TT.Value
,TT.RequestDateTime
,TT.CreatedDateTime
,TT.RequestProcessed
,TT.BorrowerResidencyType
,TT.isActive
FROM
IR_CREDITREQUEST_SPTESTING TT
INNER JOIN (
select DISTINCT
A.TaxYear,
A.[TaxPayer-SSN] AS TaxPayerSSN,
A.EngagementID AS EngagementID,
CASE
WHEN A.[Filing Status] = 'MFJ' THEN 'Joint'
ELSE A.[Filing Status]
END AS [CreditRequestType],
'' AS BorrowerID ,
A.[TaxPayer-Title/Suffix] AS NameSuffix,
A.[TaxPayer-FirstName] AS FirstName,
'' AS MiddleName,
A.[TaxPayer-LastName] AS LastName,
B.[Spouse-FirstName] + ' ' + B.[Spouse-LastName] AS [SpouseName],
B.[Spouse-SSN] AS [SpouseSSN],
CASE
WHEN A.[Filing Status] = 'MFJ' THEN 'CoBorrower'
WHEN A.[Filing Status] = 'Single' THEN 'Borrower'
ELSE A.[Filing Status]
END AS [PrintPositionType],
CASE
WHEN B.[Spouse-FirstName] IS not null THEN 'Married'
ELSE 'Unmarried'
END AS [MaritalStatusType],
C.[Address] AS StreetAddress,
C.City AS City,
C.[State] AS State,
C.[Postal Code] AS PostalCode,
A.[Primary Contact] AS Type,
A.[TaxPayer-Home/Evening Telephone Number] AS Value,
NULL AS RequestDateTime,
GETDATE() AS CreatedDateTime,
NULL AS RequestProcessed,
NULL AS BorrowerResidencyType,
0 AS IsActive
from DimTaxPayerInfo A
LEFT join DimTaxPayerSpouseInfo B on A.[TaxPayer-SSN] = B.[TaxPayer-SSN] AND B.TaxSoftwareId = 4
LEFT join stg.stg_DimTaxPayerAddress C on A.[TaxPayer-SSN] = C.[TaxPayer-SSN]
WHERE A.[TaxPayer-SSN] != ''
) as Y
ON TT.[EngagementID] = Y.[EngagementID]
) DD
ON T.[EngagementID] = DD.[EngagementID]
WHEN MATCHED THEN
Update
SET T.TaxYear = DD.TaxYear
,T.TaxPayerSSN = dd.TaxPayerSSN
,T.EngagementID = dd.EngagementID
,T.CreditRequestType = dd.CreditRequestType
,T.BorrowerID = dd.BorrowerID
,T.NameSuffix = dd.NameSuffix
,T.FirstName = dd.FirstName
,T.MiddleName = dd.MiddleName
,T.LastName = dd.LastName
,T.SpouseName = dd.SpouseName
,T.SpouseSSN = dd.SpouseSSN
,T.PrintPositionType = dd.PrintPositionType
,T.MaritalStatusType = dd.MaritalStatusType
,T.StreetAddress = dd.StreetAddress
,T.City = dd.City
,T.State = dd.State
,T.PostalCode = dd.PostalCode
,T.Type = dd.Type
,T.Value = dd.Value
,T.RequestDateTime = dd.RequestDateTime
,T.CreatedDateTime = dd.CreatedDateTime
,T.RequestProcessed = dd.RequestProcessed
,T.BorrowerResidencyType = dd.BorrowerResidencyType
,T.IsActive = dd.IsActive
WHEN NOT MATCHED THEN
INSERT
( TaxYear
,TaxPayerSSN
,EngagementID
,CreditRequestType
,BorrowerID
,NameSuffix
,FirstName
,MiddleName
,LastName
,SpouseName
,SpouseSSN
,PrintPositionType
,MaritalStatusType
,StreetAddress
,City
,State
,PostalCode
,Type
,Value
,RequestDateTime
,CreatedDateTime
,RequestProcessed
,BorrowerResidencyType
,IsActive
)
values (
dd.TaxYear
,dd.TaxPayerSSN
,dd.EngagementID
,dd.CreditRequestType
,dd.BorrowerID
,dd.NameSuffix
,dd.FirstName
,dd.MiddleName
,dd.LastName
,dd.SpouseName
,dd.SpouseSSN
,dd.PrintPositionType
,dd.MaritalStatusType
,dd.StreetAddress
,dd.City
,dd.State
,dd.PostalCode
,dd.Type
,dd.Value
,dd.RequestDateTime
,dd.CreatedDateTime
,dd.RequestProcessed
,dd.BorrowerResidencyType
,dd.IsActive
);
Your Merge Statement looks correct. This error is mostly seen in Azure Synapse. Make sure your target table is hash distributed to avoid this error.
Refer to the answer posted in the thread for a similar error.

SQL get fields which are null/empty for each record returned

I have the below query which I'm using to retrieve records which have at least one of the following columns set as NULL or '':
MainImage
Summary
Description
DECLARE #missingFields varchar(100)
SET #missingFields = ''
SELECT
c.[UnitReference] as 'UnitRef',
t.[NodeName] as 'Property',
#missingFields as 'Field/s missing'
FROM [DetailPage] d
INNER JOIN [CRM] c
ON d.ItemID = c.ItemID
INNER JOIN
[Tree] t
ON d.[ID] = t.[ID]
WHERE (ISNULL(d.MainImage, '') = '' OR ISNULL(d.Summary,'') = '' OR ISNULL(d.[Description],'') = '')
AND c.[IsListed] = 1 AND c.[IsMarketed] = 1
This returns the data I want however I also need to build up a string which lists which of the columns is null or empty for the returned record, e.g. "Main Image is empty, Description is empty" when a record has empty MainImage and Description columns.
I've tried:
#missingFields = CASE WHEN ISNULL(MainImage, '') = '' THEN 'Main image is null' ELSE '' END -- etc...
But I can't include this with the data retrieval operations. How would I go about doing this?
SELECT c.[UnitReference] AS 'UnitRef',
t.[NodeName] AS 'Property',
( CASE
WHEN d.MainImage IS NULL
THEN 'Main Image is Null, '
WHEN d.MainImage = ''
THEN 'Main Image is Empty, '
ELSE ''
END )+
( CASE
WHEN d.Summary IS NULL
THEN 'Summary is Null, '
WHEN d.Summary = ''
THEN 'Summary is Empty, '
ELSE ''
END )+
( CASE
WHEN d.[Description] IS NULL
THEN 'Description is Null, '
WHEN d.[Description] = ''
THEN 'Description is Empty, '
ELSE ''
END ) AS MissingFields
FROM [DetailPage] d
INNER JOIN [CRM] c
ON d.ItemID = c.ItemID
INNER JOIN [Tree] t
ON d.[ID] = t.[ID]
WHERE( d.MainImage IS NULL OR d.MainImage = '' )
OR ( d.Summary IS NULL OR d.Summary = '' )
OR ( d.[Description] IS NULL OR d.[Description] = '' )
AND c.[IsListed] = 1
AND c.[IsMarketed] = 1

Invalid column error in SQL Server 2008

I am trying to run the following query,
DECLARE #p_UserName as nvarchar(100)
DECLARE #p_Department as int
DECLARE #p_Section as int
DECLARE #p_SubSection as int
DECLARE #p_PermissionGroup as int
DECLARE #p_DistributionGroup as int
DECLARE #p_Permission as nvarchar(100)
DECLARE #p_IfPerChecked as bit
DECLARE #p_Role as int
SET #p_UserName = ''
SET #p_Department = NULL
SET #p_Section = NULL
SET #p_SubSection = NULL
SET #p_PermissionGroup = NULL
SET #p_DistributionGroup = NULL
SET #p_Permission = ''
SET #p_Role = NULL
SELECT Users.EnglishName,
(SELECT Designation.TitleEnglish FROM Designation WHERE Users.Designation = Designation.ID) AS [Role],
(SELECT Department.TitleEnglish FROM Department WHERE Users.DepartmentID = Department.ID) AS [Department],
(SELECT Section.TitleEnglish FROM Section WHERE Users.SectionID = Section.SectionID) AS [Section],
(SELECT SubSection.TitleEnglish FROM SubSection WHERE Users.SubSectionID = SubSection.SubSectionID) AS [Sub-Section],
(SELECT Groups.TitleEnglish FROM Groups WHERE
(SELECT UserRights.GroupID FROM UserRights WHERE Users.UserID = UserRights.UserID) = Groups.GroupID
AND Groups.IsDistribution = 0
) AS [Permissions Group],
(SELECT Groups.TitleEnglish FROM Groups WHERE
(SELECT UserRights.GroupID FROM UserRights WHERE Users.UserID = UserRights.UserID) = Groups.GroupID
AND Groups.IsDistribution = 1
) AS [Distribution Group],
(CASE
WHEN Users.ApplyUserRights = 1 THEN dbo.fn_GetUserPermissions('Users', Users.UserID)
WHEN Users.ApplyUserRights = 0 THEN dbo.fn_GetUserPermissions('Groups', (SELECT GroupID FROM UserRights WHERE Users.UserID = UserRights.UserID))
END) AS [Permissions]
FROM Users WHERE
((#p_UserName <> NULL OR #p_UserName <> '') AND #p_UserName = Users.UserName) OR
((#p_Department <> NULL OR #p_Department <> 0) AND #p_Department = Users.DepartmentID) OR
((#p_Section <> NULL OR #p_Section <> 0) AND #p_Section = Users.SectionID) OR
((#p_SubSection <> NULL OR #p_SubSection <> 0) AND #p_SubSection = Users.SubSectionID) OR
((#p_PermissionGroup <> NULL OR #p_PermissionGroup <> 0) AND #p_PermissionGroup = (
SELECT Groups.GroupID FROM Groups WHERE
(SELECT UserRights.GroupID FROM UserRights WHERE Users.UserID = UserRights.UserID) = Groups.GroupID
AND Groups.IsDistribution = 0
)) OR
((#p_DistributionGroup <> NULL OR #p_DistributionGroup <> 0) AND #p_DistributionGroup = (
SELECT Groups.GroupID FROM Groups WHERE
(SELECT UserRights.GroupID FROM UserRights WHERE Users.UserID = UserRights.UserID) = Groups.GroupID
AND Groups.IsDistribution = 1
)) OR
(1 = CASE
WHEN Users.ApplyUserRights = 1 AND #p_Permission = 'AllowChangePassword' THEN 0
ELSE dbo.fn_CheckPemission(#p_Permission, UserID)
END) OR
((#p_Role <> NULL OR #p_Role <> 0) AND #p_Role = Users.Designation) OR
(1 = 1)
GROUP BY CASE #GroupBy
WHEN 'DepartmentID' THEN Users.DepartmentID
WHEN 'SectionID' THEN Users.SectionID
WHEN 'SubSectionID' THEN Users.SubSectionID
ELSE Users.EnglishName
END
And I keep getting the error
Column 'Users.EnglishName' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Please help!
Like the message states, you need to group by the EnglishName column unless it is used in an aggregate function(Max or sum or likewise).
The issue is evident from your error message. Use GROUP BY in case of aggregate functions.

Improve case statement in order clause

I have the store sql
ALTER procedure [dbo].[TNNews_User_SearchBasic]
#Title nvarchar(400),
#CategoryId int,
#IsInterested int,
#IsHot int,
#IsTopCategory int,
#IsPublish int,
#PageSize int,
#PageIndex int,
#OrderBy varchar(20),
#PortalId int,
#LanguageId varchar(6)
as
DECLARE #EndTime DATETIME
DECLARE #StartTime DATETIME
SET #StartTime = GETDATE()
declare #tbCategory table(Id int)
DECLARE #StartRowIndex INT
IF #PageSize=0 SELECT #PageSize=count(*) FROM TNNews
IF(#PageIndex<0) SET #PageIndex=0
SET #StartRowIndex = #PageSize*(#PageIndex-1)+1
;WITH tmpCategory(Id, Name,ParentId,Level)
AS (
SELECT
e.Id,
e.Name,
e.ParentId,
1
FROM dbo.TNCategory AS e
WHERE
Id = #CategoryId or (#CategoryId='' and ParentId<=0)
UNION ALL
SELECT
e.Id,
e.Name,
e.ParentId,
Level + 1
FROM dbo.TNCategory AS e
JOIN tmpCategory AS d ON e.ParentId = d.Id
)
insert #tbCategory select Id from tmpCategory
;WITH tmpNews as
(
SELECT
a.Id,a.Title,a.Subject
,ROW_NUMBER() OVER (ORDER BY (Publisheddate) desc) as ThuTuBanGhi
FROM dbo.TNNews a
where 1 = 1
--and ( Title like '%'+#Title+'%')
and (#CategoryId = -1 or exists (select 0 from #tbCategory b where b.Id = a.CategoryId))
and (#IsInterested = -1 or IsIntrested = #IsInterested )
and (#IsHot = -1 or IsHot = #IsHot )
and (#IsTopCategory = -1 or IsTopCategory = #IsTopCategory )
and (#IsPublish = -1 or IsPublished = #IsPublish)
and PortalId=#PortalId
and LanguageId = #LanguageId
)
select *, (select COUNT(Id) from tmpNews) as 'TongSoBanGhi' from tmpNews
WHERE
ThuTuBanGhi BETWEEN (#StartRowIndex) AND (#StartRowIndex + #PageSize-1)
SET #EndTime = GETDATE()
PRINT 'StartTime = ' + CONVERT(VARCHAR(30),#StartTime,121)
PRINT ' EndTime = ' + CONVERT(VARCHAR(30),#EndTime,121)
PRINT ' Duration = ' + STR(DATEDIFF(MILLISECOND,#StartTime,#EndTime)) + ' millisecond'
select STR(DATEDIFF(MILLISECOND,#StartTime,#EndTime))
After this store excute
EXEC [dbo].[TNNews_User_SearchBasic]
#Title='',
#CategoryId = '',
#IsInterested = -1,
#IsHot = -1,
#IsTopCategory = -1,
#IsPublish = -1,
#PageSize = 20,
#PageIndex = 1,
#OrderBy = '',
#PortalId = 0,
#LanguageId = N'vi-VN'
go
The time excute about "200ms". And I create a new store "TNNews_User_SearchBasic1" with some change.
.....
--,ROW_NUMBER() OVER (ORDER BY (Publisheddate) desc) as ThuTuBanGhi
,ROW_NUMBER() OVER (ORDER BY (case when #OrderBy='VIEW_COUNT' then ViewCount else PublishedDate end) desc) as ThuTuBanGhi
.....
and now the time excute this store
EXEC [dbo].[TNNews_User_SearchBasic1]
#Title='',
#CategoryId = '',
#IsInterested = -1,
#IsHot = -1,
#IsTopCategory = -1,
#IsPublish = -1,
#PageSize = 20,
#PageIndex = 1,
#OrderBy = '',
#PortalId = 0,
#LanguageId = N'vi-VN'
GO
about 900ms.
I don't understand why there is a change. Please help me improve these stores.
PS: I put example db at: http://anhquan22.tk/Portals/0/Videos/web.rar
Finished analysis the structure of your database. The part of the problem is hiding in the table structure.
I have prepared a backup for you. In it, I slightly modified scheme to improve performance and some normalize the table. You can download it from this link.
...to your question, I would do like this -
DECLARE #SQL NVARCHAR(1000)
SELECT #SQL = N'
;WITH tmpCategory (Id, Name, ParentId, [Level]) AS
(
SELECT
e.Id
, e.Name
, e.ParentId
, 1
FROM dbo.TNCategory e
WHERE Id = #CategoryId OR (#CategoryId = '''' AND ParentId <= 0)
UNION ALL
SELECT
e.Id
, e.Name
, e.ParentId
, [Level] + 1
FROM dbo.TNCategory e
JOIN tmpCategory d ON e.ParentId = d.Id
)
SELECT
a.Id
, ROW_NUMBER() OVER (ORDER BY ' +
CASE WHEN #OrderBy = 'VIEW_COUNT'
THEN 'ViewCount'
ELSE 'PublishedDate'
END +' DESC) AS ThuTuBanGhi
FROM dbo.TNNewsMain a
where PortalId = #PortalId
AND LanguageId = #LanguageId'
+ CASE WHEN #IsInterested != -1 THEN ' AND IsInterested = #IsInterested' ELSE '' END
+ CASE WHEN #IsHot != -1 THEN ' AND IsHot = #IsHot' ELSE '' END
+ CASE WHEN #IsTopCategory != -1 THEN ' AND IsTopCategory = #IsTopCategory' ELSE '' END
+ CASE WHEN #IsPublish != -1 THEN ' AND IsPublish = #IsPublish' ELSE '' END
+ CASE WHEN #CategoryId != -1 THEN '' ELSE ' AND EXISTS(SELECT 1 FROM tmpCategory b WHERE b.Id = a.CategoryId)' END
INSERT INTO #temp (Id, ThuTuBanGhi)
EXECUTE sp_executesql
#SQL
, N'#PortalId INT
, #LanguageId VARCHAR(6)
, #CategoryId INT
, #IsInterested INT
, #IsHot INT
, #IsTopCategory INT
, #IsPublish INT'
, #PortalId = #PortalId
, #LanguageId = #LanguageId
, #CategoryId = #CategoryId
, #IsInterested = #IsInterested
, #IsHot = #IsHot
, #IsTopCategory = #IsTopCategory
, #IsPublish = #IsPublish;
SELECT
d.Id
, tm.Title
, tm.[Subject]
, d.ThuTuBanGhi
, c.TongSoBanGhi
FROM (
SELECT t.Id
, t.ThuTuBanGhi
FROM #temp t
WHERE t.ThuTuBanGhi BETWEEN #StartRowIndex AND #StartRowIndex + #PageSize - 1
) d
JOIN TNNewsMain tm ON d.Id = tm.Id
CROSS JOIN (
SELECT TongSoBanGhi = (SELECT COUNT(1) FROM #temp)
) c

Error converting data type varchar to numeric in SQL

I have spent the last 3 hours trying to figure this out but I have had not luck. When executing this SP I get the below error:
Msg 8114, Level 16, State 5, Procedure sp_SPLIT_CARTON, Line 28
Error converting data type varchar to numeric.
Here is the SP and help is really appreciated
USE [1_WMS]
GO
ALTER PROCEDURE [dbo].sp_SPLIT_CARTON
#FROM_CARTON VARCHAR(20)
, #TO_CARTON VARCHAR(20)
, #SKU VARCHAR(20)
, #QTY DECIMAL
, #USER VARCHAR(20)
AS
DECLARE
#DATE VARCHAR(10)
, #TIME VARCHAR(8)
, #SYS_CONFIG_CODE VARCHAR(5)
, #SYS_CONFIG_VALUE INT
, #CN_STATUS INT
, #CN_STATUS_1 INT --USE FOR BETWEEN STATEMENT
, #CN_STATUS_2 INT --USE FOR BETWEEN STATEMENT
, #CN_STORE VARCHAR(10)
SET #DATE = CONVERT(VARCHAR(10), GETDATE(),101);
SET #TIME = CONVERT(VARCHAR(8), GETDATE(),114);
SET #SYS_CONFIG_CODE = 'SPLCN';
SET #CN_STATUS_1 = '10';
--SET #CN_STATUS_2 = 20;
--THIS IS LINE 28
/*CHECK FOR VALID CARTON STATUS BEFORE SPLITTING*/
SELECT #CN_STATUS = cn_status
, #CN_STORE = cn_store
FROM CARTON
WHERE cn_number = #FROM_CARTON --I BELIEVE THIS IS THE PIECE OF CODE CAUSING THE ISSUE
IF #CN_STATUS = #CN_STATUS_1
BEGIN
/*CHECK FOR SYSTEM CONFIGURATION*/
SELECT #SYS_CONFIG_VALUE = sys_value FROM SYS_CONFIG
WHERE sys_code = #SYS_CONFIG_CODE
IF #SYS_CONFIG_VALUE = 1
BEGIN
/*REMOVE SKU FROM CURRENT CARTON*/
DELETE FROM CARTON_DETAIL
WHERE cd_carton_number = #FROM_CARTON
AND cd_barcode = #SKU
/*UPDATE THE CARTON HEADER*/
UPDATE CARTON
SET cn_packed_qty = cn_packed_qty - #QTY
, cn_modify_date = #DATE
, cn_modify_time = #TIME
, cn_modify_by = #USER
WHERE cn_number = #FROM_CARTON
AND cn_status BETWEEN #CN_STATUS_1 AND #CN_STATUS_2
/*CREATE NEW CARTON HEADER*/
INSERT INTO CARTON
(
cn_number
)
VALUES
(
#FROM_CARTON
)
/*CREATE CARTON DETAIL*/
INSERT INTO CARTON_DETAIL
(
cd_carton_number
)
VALUES
(
#TO_CARTON
)
END
ELSE IF #SYS_CONFIG_VALUE = 0
BEGIN
/*REMOVE SKU FROM CURRENT CARTON*/
DELETE FROM CARTON_DETAIL
WHERE cd_carton_number = #FROM_CARTON
AND cd_barcode = #SKU
/*UPDATE THE CARTON HEADER*/
UPDATE CARTON
SET cn_packed_qty = cn_packed_qty - #QTY
, cn_modify_date = #DATE
, cn_modify_time = #TIME
, cn_modify_by = #USER
WHERE cn_number = #FROM_CARTON
AND cn_status BETWEEN #CN_STATUS_1 AND #CN_STATUS_2
/*GET THE NEXT CARTON FROM COUNTERS*/
SELECT #TO_CARTON = counter_current FROM COUNTERS WHERE counter_name = (
SELECT DISTINCT so_counter_name FROM STORES WHERE SO_NUMBER = (
SELECT DISTINCT cn_store FROM CARTON WHERE cn_number = #FROM_CARTON))
/*UPDATE THE COUNTER AFTER GETTING THE NEXT CARTON NUMBER*/
UPDATE COUNTERS SET counter_current = counter_current + 1
, counter_next = counter_next + 1
WHERE counter_name = (SELECT DISTINCT so_counter_name FROM STORES WHERE SO_NUMBER = (
SELECT DISTINCT cn_store FROM CARTON WHERE cn_number = #FROM_CARTON))
/*CREATE NEW CARTON HEADER*/
DECLARE
#CN_NUMBER VARCHAR(20) , #CN_PICKTICKET VARCHAR(20) , #2ndCN_STORE VARCHAR(10) , #CN_LOAD_NUMBER VARCHAR(20)
, #CN_SHIPMENT_NUMBER VARCHAR(20) , #CN_MANIFEST_NUMBER VARCHAR(20) , #CN_PACKED_QTY DECIMAL , #CN_TRACKING_NUMBER VARCHAR(20)
, #CN_TYPE VARCHAR(5) , #CN_PACK_TYPE VARCHAR(5) , #CN_ROUTE VARCHAR(5) , #CN_SHIP_VIA VARCHAR(5)
, #CN_BOL VARCHAR(20) , #CN_MBOL VARCHAR(20) , #CN_PARCEL_NUMBER VARCHAR(10) , #CN_TRAILER_NUMBER VARCHAR(10)
, #CN_AREA VARCHAR(10) , #CN_ZONE VARCHAR(10) , #CN_AISLE VARCHAR(10) , #CN_LEVEL VARCHAR(10)
, #CN_POSITION VARCHAR(10) , #CN_HEIGHT DECIMAL , #CN_WIDTH DECIMAL , #CN_DIMENSION DECIMAL
, #CN_WEIGHT DECIMAL , #CN_VOLUME DECIMAL , #2ndCN_STATUS INT , #CN_ADDRESS VARCHAR(150)
, #CN_ADDRESS_1 VARCHAR(150) , #CN_CITY VARCHAR(50) , #CN_STATE VARCHAR(50) , #CN_ZIP_CODE VARCHAR(20)
, #CN_COUNTRY VARCHAR(50) , #CN_MISC7 VARCHAR(50) , #CN_MISC8 VARCHAR(50) , #CN_MISC9 VARCHAR(50)
, #CN_MISC10 VARCHAR(50)
SET #CN_NUMBER = #TO_CARTON SET #2ndCN_STORE = #CN_STORE SET #CN_LOAD_NUMBER = ''
SET #CN_SHIPMENT_NUMBER = '' SET #CN_MANIFEST_NUMBER = '' SET #CN_PACKED_QTY = ''
SET #CN_TRACKING_NUMBER = '' SET #CN_TYPE = 'SPLIT' SET #CN_PACK_TYPE = 'SPLITTED'
SET #CN_ROUTE = '' SET #CN_SHIP_VIA = '' SET #CN_BOL = ''
SET #CN_MBOL = '' SET #CN_PARCEL_NUMBER = '' SET #CN_TRAILER_NUMBER = ''
SET #CN_AREA = '' SET #CN_ZONE = '' SET #CN_AISLE = ''
SET #CN_LEVEL = '' SET #CN_POSITION = '' SET #CN_HEIGHT = ''
SET #CN_WIDTH = '' SET #CN_DIMENSION = '' SET #CN_WEIGHT = ''
SET #CN_VOLUME = '' SET #2ndCN_STATUS = '10' SET #CN_MISC7 = ''
SET #CN_MISC8 = '' SET #CN_MISC9 = '' SET #CN_MISC10 = ''
/*GET STORE INFORMATION*/
SELECT #CN_ADDRESS = so_address
, #CN_ADDRESS_1 = so_address_1
, #CN_CITY = so_city
, #CN_STATE = so_state
, #CN_ZIP_CODE = so_zip_code
, #CN_COUNTRY = so_country
FROM STORES
WHERE so_number = #CN_STORE
EXECUTE sp_CREATE_CARTON
#CN_NUMBER , #CN_PICKTICKET , #CN_STORE , #CN_LOAD_NUMBER , #CN_SHIPMENT_NUMBER
, #CN_MANIFEST_NUMBER , #CN_PACKED_QTY , #CN_TRACKING_NUMBER , #CN_TYPE , #CN_PACK_TYPE
, #CN_ROUTE , #CN_SHIP_VIA , #CN_BOL , #CN_MBOL , #CN_PARCEL_NUMBER
, #CN_TRAILER_NUMBER , #CN_AREA , #CN_ZONE , #CN_AISLE , #CN_LEVEL
, #CN_POSITION , #CN_HEIGHT , #CN_WIDTH , #CN_DIMENSION , #CN_WEIGHT
, #CN_VOLUME , #CN_STATUS , #CN_ADDRESS , #CN_ADDRESS_1 , #CN_CITY
, #CN_STATE , #CN_ZIP_CODE , #CN_COUNTRY , #CN_MISC7 , #CN_MISC8
, #CN_MISC9 , #CN_MISC10 , #USER
INSERT INTO CARTON
(
cn_number
)
VALUES
(
#TO_CARTON
)
/*CREATE CARTON DETAIL*/
INSERT INTO CARTON_DETAIL
(
cd_carton_number
)
VALUES
(
#TO_CARTON
)
END
END
--ELSE
-- BEGIN
-- EXECUTE sp_CREATE_ERROR_MESSAGE
-- #ER_TYPE
-- END
GO
--Find the row that is not numeric and fix data
SELECT *
FROM CARTON
WHERE ISNUMERIC(cn_number) != 1 OR
ISNUMERIC(cn_status) != 1
--If you can't fix the data you will need to cast as varchar and compare