Stored procedure how to cater NULL condition - sql

I am reading a text file as an input to execute the logic in my stored procedure following is the sample of my textfile
<tblThreatenedSpeciesSubzone>
<ThreatenedSpeciesZoneID>-1</ThreatenedSpeciesZoneID>
<ManagementZoneID>0</ManagementZoneID>
<TSSubZoneNumber>BR101_Moderate/Good_Medium_1</TSSubZoneNumber>
<TSSubZoneArea>0</TSSubZoneArea>
<AdjacentRemnantVegArea>23</AdjacentRemnantVegArea>
<PatchArea>0</PatchArea>
<CreatedBySystemUser>BBCC Training 1</CreatedBySystemUser>
<UpdatedBySystemUser>BBCC Training 1</UpdatedBySystemUser>
<SaveType>1</SaveType>
<VegetationZoneID>-1</VegetationZoneID>
<ManagementZoneName />
</tblThreatenedSpeciesSubzone>
If you noticed <ManagementZoneName /> don't have any value. In my stored procedure I tried to find if Managementzone has value or not but not sure is the right way to do
IF #ManagementZoneIDInXML > 0 and ##ManagementZoneIDInXML <> NULL
BEGIN
INSERT INTO #tblThreatenedSpeciesSubzone
(ThreatenedSpeciesZoneID,
ManagementZoneID,
VegetationZoneID,
TSSubZoneNumber,
TSSubZoneArea,
AdjacentRemnantVegArea,
PatchArea,
DateCreated,
CreatedBySystemUser,
DateUpdated,
UpdatedBySystemUser,
SaveType,
RowTimestamp)
SELECT *
FROM OPENXML (#hDoc, '/NewDataSet/tblThreatenedSpeciesSubzone', 2)
WITH (ThreatenedSpeciesZoneID INT,
ManagementZoneID INT,
VegetationZoneID INT,
TSSubZoneNumber VARCHAR(50),
TSSubZoneArea NUMERIC(9,2),
AdjacentRemnantVegArea NUMERIC(9,2),
PatchArea NUMERIC(9,2),
DateCreated VARCHAR(50),
CreatedBySystemUser VARCHAR(50),
DateUpdated VARCHAR(50),
UpdatedBySystemUser VARCHAR(50),
SaveType INT,
RowTimestamp VARCHAR(50)) XMLDATA
WHERE
ManagementZoneID = #ManagementZoneIDInXML --Only select the rows that belong to the supplied ManagementZoneID
END
ELSE
BEGIN
INSERT INTO #tblThreatenedSpeciesSubzone
( ThreatenedSpeciesZoneID ,
ManagementZoneID ,
VegetationZoneID ,
TSSubZoneNumber ,
TSSubZoneArea ,
AdjacentRemnantVegArea ,
PatchArea ,
DateCreated ,
CreatedBySystemUser ,
DateUpdated ,
UpdatedBySystemUser ,
SaveType ,
RowTimestamp )
SELECT * FROM OPENXML (#hDoc, '/NewDataSet/tblThreatenedSpeciesSubzone', 2)
WITH ( ThreatenedSpeciesZoneID INT ,
ManagementZoneID INT ,
VegetationZoneID INT ,
TSSubZoneNumber VARCHAR(50) ,
TSSubZoneArea NUMERIC(9,2) ,
AdjacentRemnantVegArea NUMERIC(9,2) ,
PatchArea NUMERIC(9,2) ,
DateCreated VARCHAR(50) ,
CreatedBySystemUser VARCHAR(50) ,
DateUpdated VARCHAR(50) ,
UpdatedBySystemUser VARCHAR(50) ,
SaveType INT ,
RowTimestamp VARCHAR(50) ) XMLDATA
WHERE VegetationZoneID = #VegetationZoneIDInXML --Only select the rows that belong to the supplied VegetationZoneID
--And no Management zone assigned
END

Related

Why primary key is getting repeated in the child tables as foreign key?

I am going nuts and I mean i cannot figure out since so many days what's happening.
Client is using this and inserting data in to the tables.
EQ_HEADER is the table and after puting data into that we take it's PRIMARY key EQ_HEADERID and put in other tables.
But PROBLEM is
In some cases.... the PRIMARY KEY that we get from PARENT table is getting duplicated in the all other child tables even the EQUIPMENTNO is different. It's 1 to 1 only but why HEADERID is getting repeated in many cases?
ALTER PROCEDURE [dbo].[InsertEquipmentsDATA]
#FunctionalLocation varchar(30)= null ,
#EquipmentNo varchar(30) = null ,
#IsGeneralEmpty bit=0,
#ANSDTSpecified bit= null,
#ANSDT_General date= null,
#ANSWT_General varchar(13)= null,
#BAUJJ_General varchar(4)= null,
#BAUMM_General varchar(2)= null,
#BEGRU_General varchar(4)= null,
#EQART_General varchar(10)= null,
#ERDAT_General date= null,
#ERDATSpecified bit= null,
#HERLD_General varchar(3)= null,
#HZEIN_General varchar(30)= null,
#HERST_General varchar(30)= null,
#INBDT_General date= null,
#INBDTSpecified bit= null,
#SERGE_General varchar(30)= null,
#TYPBZ_General varchar(20)= null,
#IsHeaderEmpty bit=0,
#DATAB_Header date= null,
#DATABSpecified bit= null,
#DATABI_Header date= null,
#DATABISpecified bit= null,
#EQTYP varchar(1)= null,
#EQUNR varchar(18)= null,
#GUID_Header varchar(36)= null,
#SHTXT_Header varchar(40)= null,
#STTXT_Header varchar(4)= null,
#IsLocationEmpty bit=0,
#ABCKZ_Location varchar(1)= null,
#BEBER_Location varchar(3)= null,
#EQFNR_Location varchar(30)= null,
#MSGRP_Location varchar(8)= null,
#STORT_Location varchar(10)= null,
#SWERK_Location varchar(4)= null,
#IsOrganizationEmpty bit= 0,
#ANLNR_Organizations varchar(12)= null,
#BUKRS_Organizations varchar(4)= null,
#GEWRK_Organizations varchar(20)= null,
#GSBER_Organizations varchar(4)= null,
#INGRP_Organizations varchar(3)= null,
#IWERK_Organizations varchar(4)= null,
#KOKRS_Organizations varchar(4)= null,
#KOSTL_Organizations varchar(10)= null,
#PROID_Organizations varchar(20)= null,
#RBNR_Organizations varchar(9)= null,
--#ATINN_Classification int,
--#ATWRT_Classification varchar(70),
#IsStructureEmpty bit=0,
#HEQNR_Structure varchar(4)= null,
#HEQUI_Structure varchar(18)= null,
#POSNR_Structure varchar(4)= null,
#SUBMT_Structure varchar(40)= null,
#TIDNR_Structure varchar(25)= null,
#TPLNR_Structure varchar(40)= null,
#ATNAM_Characteristic varchar(50)= null,
#ATZHL_Characteristic varchar(3)= null,
#ATBEZ_Characteristic varchar(40)= null,
#ATWRT_Value varchar(40)= null,
#EINHE_Value varchar(6)= null ,
#ATZHL_Value varchar(3)= null ,
#IsSerialEmpty bit= 0,
#B_CHARGE varchar(10)= null,
#B_LAGER varchar(4)= null,
#B_WERK varchar(4)= null ,
#CHARGE varchar(10) = null ,
#LBBSA varchar(2) = null ,
#MATNR varchar(40) = null ,
#SERNR varchar(18) = null ,
#IsWarrantyEmpty bit= 0,
#GWLDT date= null ,
#GWLDTSpecified bit= null,
#GWLEN date = null ,
#GWLENSpecified bit= null,
#ClassificationCollection Classification readonly,
#CharacteristicCollection Characteristic readonly,
#ValueCollection Value readonly,
#StatusRet varchar(20) out,
#ErrorRet varchar(1000) out
AS
BEGIN
Begin Try
Begin Transaction
Declare #Status Char(1)
SET #Status='N'
IF exists (Select * from EQ_Header where EQUNR= #EQUNR)
Begin
Set #StatusRet= 'Change'
Set #ErrorRet= ''
SET #Status='U'
End
-- --Update EQ_Header
-- --Set
-- -- EquipmentNo = #EquipmentNo ,
-- -- DATAB = #DATAB_Header,
-- -- DATABSpecified = #DATABSpecified ,
-- -- DATBI = #DATABI_Header ,
-- -- DATBISpecified = #DATABISpecified ,
-- -- EQTYP = #EQTYP ,
-- -- EQUNR = #EQUNR ,
-- -- SHTXT = #SHTXT_Header ,
-- -- STTXT = #STTXT_Header
-- --where EQUNR= #EQUNR
-- Set #STATUSRet= 'Change'
-- Set #ErrorRet= (Select ERROR_MESSAGE())
-- Commit
-- return
--End
Insert into FunctionalLocations
(
FunctionalLocation
)
values
(
#FunctionalLocation
)
Insert into [dbo].[Equipment]
(
EquipmentNo,
FunctionalLocation,
Status
)
values
(
#EQUNR,
#FunctionalLocation,
'Pending'
)
IF(#IsHeaderEmpty = 0)
Begin
Insert into [dbo].[EQ_Header]
(
EquipmentNo ,
DATAB ,
DATABSpecified ,
DATBI ,
DATBISpecified ,
EQTYP ,
EQUNR ,
SHTXT ,
STTXT,
GUID,
Status,
Transferred
)
values
(
#EquipmentNo ,
#DATAB_Header ,
#DATABSpecified ,
#DATABI_Header ,
#DATABISpecified ,
#EQTYP ,
#EQUNR ,
#SHTXT_Header,
#STTXT_Header,
#GUID_Header,
#Status,
0
)
End
Declare #HeaderID int
Set #HeaderID= IDENT_CURRENT('EQ_Header') --Latest Header ID
IF(#IsGeneralEmpty = 0)
Begin
Insert into [dbo].[EQ_General]
(
EquipmentNo ,
ANSDT ,
ANSDTSpecified ,
ANSWT ,
BAUJJ ,
BAUMM ,
BEGRU ,
EQART ,
ERDAT ,
ERDATSpecified ,
HERLD ,
HERST ,
HZEIN ,
INBDT ,
INBDTSpecified ,
SERGE ,
TYPBZ ,
HeaderID
)
values
(
#EquipmentNo ,
#ANSDT_General ,
#ANSDTSpecified ,
#ANSWT_General ,
#BAUJJ_General ,
#BAUMM_General ,
#BEGRU_General ,
#EQART_General ,
#ERDAT_General ,
#ERDATSpecified ,
#HERLD_General ,
#HERST_General ,
#HZEIN_General,
#INBDT_General ,
#INBDTSpecified ,
#SERGE_General ,
#TYPBZ_General ,
#HeaderID
)
End
IF(#IsLocationEmpty = 0)
Begin
Insert into [dbo].[EQ_Location]
(
EquipmentNo,
ABCKZ ,
BEBER ,
EQFNR ,
MSGRP ,
STORT ,
SWERK,
HeaderID
)
values
(
#EquipmentNo,
#ABCKZ_Location ,
#BEBER_Location ,
#EQFNR_Location ,
#MSGRP_Location ,
#STORT_Location ,
#SWERK_Location,
#HeaderID
)
End
IF(#IsOrganizationEmpty = 0)
Begin
Insert into [dbo].[EQ_Organizations]
(
EquipmentNo,
ANLNR ,
BUKRS ,
GEWRK ,
GSBER ,
INGRP ,
IWERK ,
KOKRS ,
KOSTL ,
PROID ,
RBNR ,
HeaderID
)
values
(
#EquipmentNo,
#ANLNR_Organizations ,
#BUKRS_Organizations ,
#GEWRK_Organizations ,
#GSBER_Organizations ,
#INGRP_Organizations ,
#IWERK_Organizations ,
#KOKRS_Organizations ,
#KOSTL_Organizations ,
#PROID_Organizations ,
#RBNR_Organizations ,
#HeaderID
)
End
IF(#IsStructureEmpty = 0)
Begin
Insert into [dbo].[EQ_Structure]
(
EquipmentNo,
HEQNR ,
HEQUI ,
POSNR ,
SUBMT ,
TIDNR ,
TPLNR,
HeaderID
)
values
(
#EquipmentNo,
#HEQNR_Structure ,
#HEQUI_Structure ,
#POSNR_Structure ,
#SUBMT_Structure ,
#TIDNR_Structure ,
#TPLNR_Structure,
#HeaderID
)
End
IF(#IsSerialEmpty = 0)
Begin
Insert into [dbo].[EQ_Serial]
(
B_CHARGE,
B_LAGER ,
B_WERK ,
CHARGE ,
LBBSA ,
MATNR ,
SERNR,
HeaderID
)
values
(
#B_CHARGE,
#B_LAGER ,
#B_WERK ,
#CHARGE ,
#LBBSA ,
#MATNR ,
#SERNR,
#HeaderID
)
End
IF(#IsWarrantyEmpty = 0)
Begin
Insert into [dbo].[EQ_Warranty]
(
EquipmentNo ,
GWLDT ,
GWLDTSpecified ,
GWLEN ,
GWLENSpecified,
HeaderID
)
values
(
#EquipmentNo ,
#GWLDT ,
#GWLDTSpecified ,
#GWLEN ,
#GWLENSpecified,
#HeaderID
)
End
Declare #ClassificationTypeCount int
Set #ClassificationTypeCount= (Select Count(*) from #ClassificationCollection)
If(#ClassificationTypeCount > 0)
Begin
--Classification
Insert into [dbo].Eq_Classification
(
EquipmentNo,
CLASS,
KLTXT,
KLART,
HeaderID
)
--values
--(
-- #EquipmentNo,
-- #CLASS_Classification,
-- #KLTXT_Classification,
-- #KLART_Classification
--)
Select #EquipmentNo, CLASS, KLTXT, KLART, #HeaderID from #ClassificationCollection
Declare #ClassificationID int
Set #ClassificationID= IDENT_CURRENT('Eq_Classification')
END
--Characteristic
Declare #CharacteristicTypeCount int
Set #CharacteristicTypeCount= (Select Count(*) from #CharacteristicCollection)
If(#CharacteristicTypeCount > 0)
Begin
Insert into [dbo].EQ_Characteristic
(
EquipmentNo,
ATNAM,
ATZHL,
ATBEZ,
HeaderID,
ClassificationID
)
--values
--(
-- #ATNAM_Characteristic,
-- #ATZHL_Characteristic,
-- #ATBEZ_Characteristic
--)
Select #EquipmentNo , ATNAM, ATZHL, ATBEZ, #HeaderID, #ClassificationID from #CharacteristicCollection
END
--Value
Declare #ValueTypeCount int
Set #ValueTypeCount= (Select Count(*) from #ValueCollection)
If(#ValueTypeCount > 0)
Begin
Insert into [dbo].EQ_Value
(
EquipmentNo,
ATWRT,
EINHE,
ATZHL,
HeaderID,
ClassificationID
)
--values
--(
-- #EquipmentNo,
-- #ATWRT_Value,
-- #EINHE_Value,
-- #ATZHL_Value
--)
Select #EquipmentNo, ATWRT, EINHE, ATZHL, #HeaderID, #ClassificationID from #ValueCollection
End
IF(ISNULL(#StatusRet , '') = '')
Begin
Set #STATUSRet= 'Created'
Set #ErrorRet= ''
End
Commit Transaction
End Try
Begin Catch
Set #STATUSRet= 'Failed'
Set #ErrorRet= ( Select CAST(Error_Line() as Varchar(20)) + ':' +ERROR_MESSAGE())
Rollback Transaction
End Catch
END
I would suggest using SCOPE_IDENTITY rather than IDENT_CURRENT. SCOPE_IDENTITY will give you the latest identity value inserted for the current session and current scope. Whereas IDENT_CURRENT will give you the value from the table across all sessions. If you have multiple clients calling this procedure concurrently it's possible that you could pick up the identity value from a different client.
I'm afraid your sproc is too long to totally parse like this, but I suspect that if this problem is intermittent, then you might find that the error is to do with this line:
Set #HeaderID= IDENT_CURRENT('EQ_Header') --Latest Header ID
You should probably read this:
https://learn.microsoft.com/en-us/sql/t-sql/functions/ident-current-transact-sql?view=sql-server-ver15
And specifically this section:
IDENT_CURRENT is similar to the SQL Server 2000 (8.x) identity
functions SCOPE_IDENTITY and ##IDENTITY. All three functions return
last-generated identity values. However, the scope and session on
which last is defined in each of these functions differ:
IDENT_CURRENT returns the last identity value generated for a specific
table in any session and any scope.
##IDENTITY returns the last
identity value generated for any table in the current session, across
all scopes.
SCOPE_IDENTITY returns the last identity value generated
for any table in the current session and the current scope.
You are currently getting the last generated ID for that table for any scope and any session. You should probably change it to SCOPE_IDENTITY.

How to pass constant value in stored procedure

I have the following statement inside a stored procedure:
INSERT INTO #tblThreatenedSpeciesSubzone
(ThreatenedSpeciesZoneID,
ManagementZoneID,
VegetationZoneID)
SELECT *
FROM OPENXML (#hDoc, '/NewDataSet/tblThreatenedSpeciesSubzone', 2)
WITH (ThreatenedSpeciesZoneID INT,
ManagementZoneID INT,
VegetationZoneID INT) XMLDATA
WHERE VegetationZoneID = #VegetationZoneIDInXML
Sometime "managementZone id" becomes null, I want to replace if it is null then I want pass the constant value "0". Is there a way to do ?
Not sure about "passing a constant value", but you can always set a default:
CREATE PROCEDURE Sales.uspGetSalesYTD
#SalesPerson nvarchar(50) = 'Andersson' -- default value
AS
So:
#ManagementZoneID INT = 0
You could use a coalesce in the select statement
INSERT INTO #tblThreatenedSpeciesSubzone
( ThreatenedSpeciesZoneID ,
ManagementZoneID ,
VegetationZoneID
)
SELECT
ThreatenedSpeciesZoneID ,
COALESCE(ManagementZoneID,0) ,
VegetationZoneID
FROM OPENXML (#hDoc, '/NewDataSet/tblThreatenedSpeciesSubzone', 2)
WITH ( ThreatenedSpeciesZoneID INT ,
ManagementZoneID INT ,
VegetationZoneID INT ) XMLDATA
WHERE VegetationZoneID = #VegetationZoneIDInXML

Why this Where condition returns 0 Rows?

SELECT
*
FROM tblName
WHERE mode = '1' AND (category = #Category OR #Category = 'all' OR NewsId = #Category)
I passed #Category='all'. This returns 0 rows. If OR NewsId=#Category this condition not added, query will return all results
Try this one -
mode = '1'
AND
(
#Category = 'all'
OR
#Category IN (category, CAST(NewsId AS VARCHAR(10)))
)
Do not use too large data-type length (i mean MAX):
CREATE TABLE dbo.TBL_ContentsPage
(
NewsId INT IDENTITY(1001,1) NOT NULL PRIMARY KEY
, Header NVARCHAR(1024) NULL
, SmallImage IMAGE NULL
, TextContent NVARCHAR(2048) NULL
, PostedDate DATETIME NOT NULL DEFAULT(GETDATE())
, mode VARCHAR(50) NULL
, [status] VARCHAR(50) NULL
, category VARCHAR(200) NULL
, author NVARCHAR(1024) NULL
, imgRefID VARCHAR(50) NULL
)
ALTER PROCEDURE [dbo].[SPGetArticlePaging]
(
#startposition INT
, #stopposition INT
, #Category VARCHAR(200)
)
AS BEGIN
SELECT
NewsId
, Header
, TextContent
, author
, PostedDate
, category
, imgRefID
FROM (
SELECT
NewsId
, Header
, TextContent
, author
, PostedDate
, category
, DateRank = ROW_NUMBER() OVER(ORDER BY PostedDate DESC)
, imgRefID
FROM dbo.TBL_ContentsPage
WHERE mode = '1'
AND
(
#Category = 'all'
OR
#Category IN (category, CAST(NewsId AS VARCHAR(10)))
)
) t
WHERE DateRank BETWEEN #startposition AND #stopposition
RETURN 0
END

Trouble with OUTPUT statement. Incorrect Syntax

I am trying to update a data set based on one conditional and then retrieving all the updated rows. VS keeps telling me there is an incorrect syntax error near my OUTPUT clause but I do not see anything wrong. I am just trying to figure out how to use "OUTPUT" so this may be a very stupid mistake I am making but failing to see.
What is wrong (syntactically) with this OUTPUT clause?
CREATE PROCEDURE [dbo].[GetInitialSessionNotifications]
#CurrentSessionId bigint
AS
DECLARE #tempTable table(
id bigint NOT NULL,
[Type] nvarchar,
DocumentCommentID bigint,
AnnouncmentID int,
EventID int,
MeetingID int,
[Read] bit,
RecieverId int,
AnnouncmentCommentId bigint,
EventCommentId bigint,
MeetingCommentId bigint,
DateAndTime DateTime);
UPDATE Notifications SET SessionId = #CurrentSessionId
WHERE SessionId != #CurrentSessionId
OUTPUT INSERTED.id,
INSERTED.[Type],
INSERTED.DocumentCommentID,
INSERTED.AnnouncmentID,
INSERTED.EventID,
INSERTED.MeetingID,
INSERTED.[Read],
INSERTED.RecieverId,
INSERTED.AnnouncmentCommentId,
INSERTED.EventCommentId,
INSERTED.MeetingCommentId,
INSERTED.DateAndTime
INTO #tempTable;
SELECT id, [Type], DocumentCommentId, AnnouncmentID, EventID, MeetingID,
[Read], RecieverId, AnnouncmentCommentId, EventCommentId, MeetingCommentId, DateAndTime
FROM #tempTable;
RETURN 0
Try this one -
CREATE PROCEDURE [dbo].[GetInitialSessionNotifications]
#CurrentSessionId BIGINT
AS BEGIN
DECLARE #tempTable TABLE
(
id BIGINT NOT NULL ,
[Type] NVARCHAR ,
DocumentCommentID BIGINT ,
AnnouncmentID INT ,
EventID INT ,
MeetingID INT ,
[Read] BIT ,
RecieverId INT ,
AnnouncmentCommentId BIGINT ,
EventCommentId BIGINT ,
MeetingCommentId BIGINT ,
DateAndTime DATETIME
)
UPDATE Notifications
SET SessionId = #CurrentSessionId
OUTPUT
INSERTED.id ,
INSERTED.[Type] ,
INSERTED.DocumentCommentID ,
INSERTED.AnnouncmentID ,
INSERTED.EventID ,
INSERTED.MeetingID ,
INSERTED.[Read] ,
INSERTED.RecieverId ,
INSERTED.AnnouncmentCommentId ,
INSERTED.EventCommentId ,
INSERTED.MeetingCommentId ,
INSERTED.DateAndTime
INTO #tempTable
WHERE SessionId != #CurrentSessionId
SELECT id ,
[Type] ,
DocumentCommentId ,
AnnouncmentID ,
EventID ,
MeetingID ,
[Read] ,
RecieverId ,
AnnouncmentCommentId ,
EventCommentId ,
MeetingCommentId ,
DateAndTime
FROM #tempTable;
END

SQL stored procedure not grouping rows together

I have the below code. It basically takes values from a spreadsheet loaded in by an asp.net upload control. A document number is automatically assigned. I put the data into a temp table, and then select and insert the data from the temp table into my actual database table. The data is group so that it takes the minimum doc number, and date column (the ONLY columns that are different for the data). The data loads fine but for some reason it does not get grouped. Does anyone see any problems with the query itself?
CREATE Procedure dbo.temptable
(
#DocumentBranchPlant varchar(12)
, #DocumentType varchar(2)
, #DEANumber varchar(9)
, #DebitMemo varchar(25)
, #DebitTotal float
, #ErrorOverRide bit
, #OnHold bit
, #LastModifiedUser varchar(50)
, #ResubmissionCode char(2)
, #DocumentNumber float out
)
AS
begin
declare #TransmissionDate datetime
declare #JulianTransmissionDate numeric(18,0)
declare #ShipTo float
declare #CustomerName varchar(40)
Set #DocumentNumber = scope_identity()
set #TransmissionDate = getdate()
set #JulianTransmissionDate =getdate()
DECLARE #cb table
( DocumentBranchPlant char(12)
, DocumentNumber float
, DocumentType char(2)
, JulianTransmissionDate numeric(18,0)
, TransmissionDate datetime
, DEANumber varchar(9)
, ShipTo float
, DebitMemo char(25)
, DebitTotal float
, CustomerName varchar(40)
, ErrorOverRide bit
, EntryComplete bit
, OnHold bit
, ManualEntry bit
, LastModifiedUser varchar(50)
, LastModifiedDate datetime
, ResubmissionCode char(2))
INSERT INTO #cb
( DocumentBranchPlant
, DocumentNumber
, DocumentType
, JulianTransmissionDate
, TransmissionDate
, DEANumber
, ShipTo
, DebitMemo
, DebitTotal
, CustomerName
, ErrorOverRide
, EntryComplete
, OnHold
, ManualEntry
, LastModifiedUser
, LastModifiedDate
, ResubmissionCode)
VALUES
( #DocumentBranchPlant
, #DocumentNumber
, #DocumentType
, #JulianTransmissionDate
, #TransmissionDate
, #DEANumber
, #ShipTo
, #DebitMemo
, #DebitTotal
, #CustomerName
, #ErrorOverRide
, 0
, #OnHold
, 1
, #LastModifiedUser
, getdate()
, #ResubmissionCode)
INSERT INTO dbo.CbTempTable
( DocumentBranchPlant
, DocumentNumber
, DocumentType
, JulianTransmissionDate
, TransmissionDate
, DEANumber
, ShipTo
, DebitMemo
, DebitTotal
, CustomerName
, ErrorOverRide
, EntryComplete
, OnHold
, ManualEntry
, LastModifiedUser
, LastModifiedDate
, ResubmissionCode)
SELECT
DocumentBranchPlant
, min(DocumentNumber)
, DocumentType
, JulianTransmissionDate
, min(TransmissionDate)
, DEANumber
, ShipTo
, DebitMemo
, DebitTotal
, CustomerName
, ErrorOverRide
, EntryComplete
, OnHold
, ManualEntry
, LastModifiedUser
, min(LastModifiedDate)
, ResubmissionCode
FROM #cb
GROUP BY
DocumentBranchPlant
, DocumentType
, JulianTransmissionDate
, DEANumber
, ShipTo
, DebitMemo
, DebitTotal
, CustomerName
, ErrorOverRide
, EntryComplete
, OnHold
, ManualEntry
, LastModifiedUser
, ResubmissionCode