This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Any way to SQLBulkCopy “insert or update if exists”?
I am using SQLBulkCopy to insert Bulk records
How can I perform on update (rather than an insert) on records that already exist? Is this possible with SQLBulkCopy?
This is my code for SQLBulkCopy
using (var bulkCopy = new SqlBulkCopy(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString, SqlBulkCopyOptions.KeepNulls & SqlBulkCopyOptions.KeepIdentity))
{
bulkCopy.BatchSize = CustomerList.Count;
bulkCopy.DestinationTableName = "dbo.tCustomers";
bulkCopy.ColumnMappings.Clear();
bulkCopy.ColumnMappings.Add("CustomerID", "CustomerID");
bulkCopy.ColumnMappings.Add("FirstName", "FirstName");
bulkCopy.ColumnMappings.Add("LastName", "LastName");
bulkCopy.ColumnMappings.Add("Address1", "Address1");
bulkCopy.ColumnMappings.Add("Address2", "Address2");
bulkCopy.WriteToServer(CustomerList);
}
Application Details
ASP.net MVC 3.0 Razor view Engine
SQL Server 2008
Thanks to #pst
with his suggestions this is how I implemented, if anyone has to implement similar.
Bulk Insert in to permanent Temp Table
using (var bulkCopy = new SqlBulkCopy(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString, SqlBulkCopyOptions.KeepNulls & SqlBulkCopyOptions.KeepIdentity))
{
bulkCopy.BatchSize = CustomerList.Count;
bulkCopy.DestinationTableName = "dbo.tPermanentTempTable";
bulkCopy.ColumnMappings.Clear();
bulkCopy.ColumnMappings.Add("CustomerID", "CustomerID");
bulkCopy.ColumnMappings.Add("FirstName", "FirstName");
bulkCopy.ColumnMappings.Add("LastName", "LastName");
bulkCopy.ColumnMappings.Add("Address1", "Address1");
bulkCopy.ColumnMappings.Add("Address2", "Address2");
bulkCopy.WriteToServer(CustomerList);
}
Then call a stored Procedure to Merge the temp table with actual table
using (Entities context = new Entities())
{
System.Nullable<int> iReturnValue = context.usp_Customer_BulkUploadMerge(customerid, locationID).SingleOrDefault();
if (iReturnValue.HasValue)
{
// return was successful!
}
}
This is how I used Merge in my Stored Procedure
ALTER PROCEDURE usp_Customer_BulkUploadMerge
(
#CustomerID INT ,
#locationID INT
)
AS
BEGIN
DECLARE #retValue INT
BEGIN TRY
IF OBJECT_ID('tCustomers') IS NOT NULL
BEGIN
BEGIN TRANSACTION MergPatientsTable
SET NOCOUNT ON;
MERGE dbo.tCustomers AS target
USING
( SELECT PU.CustomerID ,
PU.LocationID ,
PU.FirstName ,
PU.LastName ,
PU.MiddleInitial ,
PU.Gender ,
PU.DOB
FROM dbo.tPermanentTempTable PU
WHERE PU.CustomerID = #CustomerID
AND PU.LocationID = #locationID
GROUP BY PU.CustomerID ,
PU.LocationID ,
PU.FirstName ,
PU.LastName ,
PU.MiddleInitial ,
PU.Gender ,
PU.DOB
) AS source ( CustomerID, LocationID, FirstName,
LastName, MiddleInitial, Gender, DOB )
ON ( LOWER(target.FirstName) = LOWER(source.FirstName)
AND LOWER(target.LastName) = LOWER(source.LastName)
AND target.DOB = source.DOB
)
WHEN MATCHED
THEN
UPDATE SET
MiddleInitial = source.MiddleInitial ,
Gender = source.Gender,
LastActive = GETDATE()
WHEN NOT MATCHED
THEN
INSERT (
CustomerID ,
LocationID ,
FirstName ,
LastName ,
MiddleInitial ,
Gender ,
DOB ,
DateEntered ,
LastActive
) VALUES
( source.CustomerID ,
source.LocationID ,
source.FirstName ,
source.LastName ,
source.MiddleInitial ,
source.Gender ,
source.DOB ,
GETDATE() ,
NULL
);
DELETE PU
FROM dbo.tPermanentTempTable PU
WHERE PU.CustomerID = #CustomerID
AND PU.LocationID = #locationID
COMMIT TRANSACTION MergPatientsTable
SET #retValue = 1
SELECT #retValue
END
ELSE
BEGIN
SET #retValue = -1
SELECT #retValue
END
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION MergPatientsTable
DECLARE #ErrorMsg VARCHAR(MAX);
DECLARE #ErrorSeverity INT;
DECLARE #ErrorState INT;
SET #ErrorMsg = ERROR_MESSAGE();
SET #ErrorSeverity = ERROR_SEVERITY();
SET #ErrorState = ERROR_STATE();
SET #retValue = 0
SELECT #retValue
-- SELECT 0 AS isSuccess
END CATCH
END
Related
For the AP Bill screen AP301000 I want to show the Customer for each line item in the 'Document Details' grid. To do this I am creating a customisation project with a custom virtual attribute on the APTran table.
I have tried many permutations of the attribute, the below is the latest.
[PXString]
[PXUIField(DisplayName="Customer", Enabled=false)]
[PXDBScalar(typeof(Search2<BAccountR.legalName,
InnerJoin<PMProject, On<PMProject.contractID, Equal<APTran.projectID>>,
InnerJoin<AR.Customer, On<PMProject.customerID, Equal<AR.Customer.bAccountID>>,
InnerJoin<BAccountR, On<AR.Customer.bAccountID, Equal<BAccountR.bAccountID>>>>>>))]
It 'should' return the customer legal name from the BAccount table through the relevant joins. However it returns a vendor name instead (not in the customer table).
The generated SQL code is below, the system has made a mess of it. For some reason it has an inner join to the vendor table, which is plainly not in my PXDBScalar attribute decoration.
How can I adjust the attribute decoration to obtain the customer name?
(SELECT TOP (1) [BAccount_BAccount].[legalname]
FROM ( [vendor] [BAccount_Vendor]
INNER JOIN [baccount] [BAccount_BAccount]
ON ( [BAccount_BAccount].[companyid] = 2 )
AND
[BAccount_BAccount].[deleteddatabaserecord] = 0
AND [BAccount_Vendor].[baccountid] =
[BAccount_BAccount].[baccountid]
LEFT JOIN [fsxvendor] [BAccount_FSxVendor]
ON ( [BAccount_FSxVendor].[companyid] = 2 )
AND
[BAccount_FSxVendor].[deleteddatabaserecord] = 0
AND [BAccount_Vendor].[baccountid] =
[BAccount_FSxVendor].[baccountid] )
INNER JOIN [contract] [PMProject]
ON ( [PMProject].[companyid] IN ( 1, 2 )
AND 8 = Substring([PMProject].[companymask],
1, 1) & 8 )
AND [PMProject].[deleteddatabaserecord] = 0
AND ( [PMProject].[companyid] IN ( 1, 2 )
AND 8 = Substring(
[PMProject].[companymask], 1, 1)
& 8
)
AND [PMProject].[deleteddatabaserecord] = 0
AND [PMProject].[contractid] =
[APTran_APTran].[projectid]
INNER JOIN ( [customer] [Customer_Customer]
INNER JOIN [baccount] [Customer_BAccount]
ON ( [Customer_BAccount].[companyid]
= 2 )
AND
[Customer_BAccount].[deleteddatabaserecord]
= 0
AND
[Customer_Customer].[baccountid] =
[Customer_BAccount].[baccountid]
LEFT JOIN [fsxcustomer]
[Customer_FSxCustomer]
ON (
[Customer_FSxCustomer].[companyid] = 2 )
AND
[Customer_FSxCustomer].[deleteddatabaserecord] = 0
AND [Customer_Customer].[baccountid] =
[Customer_FSxCustomer].[baccountid] )
ON [PMProject].[customerid] = [Customer_BAccount].[baccountid]
AND ( [Customer_Customer].[companyid] = 2 )
AND [Customer_Customer].[deleteddatabaserecord] = 0
INNER JOIN [baccount] [BAccountR]
ON ( [BAccountR].[companyid] = 2 )
AND [BAccountR].[deleteddatabaserecord] = 0
AND ( [BAccountR].[companyid] = 2 )
AND [BAccountR].[deleteddatabaserecord] = 0
AND [Customer_BAccount].[baccountid] =
[BAccountR].[baccountid]
WHERE ( [BAccount_Vendor].[companyid] = 2 )
AND [BAccount_Vendor].[deleteddatabaserecord] = 0
ORDER BY [BAccount_BAccount].[legalname]) AS [UsrCustomerName],
Try creating a new class for the BAccountR table and using the PXBreakInheritance attribute, and then use this new table to do your lookup.
namespace Custom.Extension
{
[PXBreakInheritance]
public class CustomBAccountR: BAccountR { }
public sealed class APTranExtension : PXCacheExtension<APTran>
{
#region ProjectCustomerID
public abstract class projectCustomerID : BqlInt.Field<projectCustomerID> { }
[PXInt]
[PXUIField(DisplayName="Customer Legal Name", Enabled=false)]
[PXSelector(typeof(SearchFor<CustomBAccountR.bAccountID>),
SubstituteKey = typeof(CustomBAccountR.acctCD),
DescriptionField = typeof(CustomBAccountR.legalName))]
[PXDBScalar(typeof(Search2<CustomBAccountR.bAccountID,
InnerJoin<PMProject,
On<PMProject.contractID.IsEqual<APTran.projectID>>,
InnerJoin<CustomBAccountR,
On<CustomBAccountR.bAccountID.IsEqual<PMProject.customerID>>>>,
Where<CustomBAccountR.bAccountID.IsNotNull>>))]
public int? ProjectCustomerID { get; set; }
#endregion
}
}
You can now use the description field in your ASPX to display out the value.
I have database in MS Access 2016, and want to run update query thru Excel VBA - this works. After this I want to return details of updated row (values from two columns - ID, Col1). Now I have only number of affected rows. How to achive this?
My query:
UPDATE
(SELECT TOP 1 ID, Col1, Update_time, Update_user
FROM Table1
WHERE Update_user Is Null
ORDER BY Col2 DESC , ID) AS U_ROW
SET U_ROW.Update_time = Now(), U_ROW.Update_user = [username];
In Excel VBA I run it thru ADODB.Command:
With baseRecordsetCommand
.ActiveConnection = objectConnection
.CommandType = adCmdStoredProc
.CommandText = "qryTest"
.NamedParameters = True
.Parameters.Append .CreateParameter("#username", adVarChar, adParamInput, 255, LCase(Environ("Username")))
.Execute recordsAffected
End With
Usually these kind of ops goes into a stored procedure but in Ms Acces/Excel you have to use VBA. Easiest way is, to retrieve the Id you are about to update before you the update.
So this comes first: save it in a variable
SELECT TOP 1 ID
FROM Table1
WHERE Update_user Is Null
ORDER BY Col2 DESC , ID
And then
update Table1
SET Table1.Update_time = Now(), Table1.Update_user = #username where Id = #idFromFirstQuery;
followed by
select * from Table1 where Id = #idFromFirstQuery;
in this way you know what Id, row you are updating.
OR:
you can turn the now() also into a parameter and supply a timestamp as parameter.
like:
update (table selection )
set update_time = '2020-19-03 10:0=10:10', update_user = 'User1';
after update you can do something like:
Select *
from Table1
where update_user = 'User1' and update_time = '2020-19-03 10:0=10:10';
NOTE!! you are highly assuming the timestamp you are supplying + username is unique. I personally would not use this method.
Not sure on this, but if you have a unique id, you could do something like this. This was done in Access, so the connection would need to be set up, as i believe this is ok.
Function sqlret() As String
Dim c As ADODB.Connection
Dim s As String
Dim r As ADODB.Recordset
Dim l As Long
Set c = CurrentProject.Connection
c.Execute "insert into tblprojects(projecttitle) values ('code1')"
Set r = New ADODB.Recordset
r.Open "select ##identity", c, 1
l = r.Fields(0).Value
r.Close
r.Open "select * from tblprojects where [projectid]=" & l, c, 1
sqlret = r.GetString
End Function
Not able to extract table names used within with clause, I'm using presto-parser version 0.226.
SqlParser sqlParser = new SqlParser();
String sql = "WITH dataset AS ( SELECT ROW('Bob', 38) AS users from tabb ) SELECT * FROM dataset";
Query query = (Query)sqlParser.createStatement(sql, ParsingOptions.builder().build());
QuerySpecification body = (QuerySpecification)query.getQueryBody();
System.out.println("From = " + body.getFrom().get());
/* Output
From = Table{dataset}
*/
Expected output
From = Table{dataset, tabb}
This code results in timeout exception
String City_Code = null;
var result = (from r in myContext.TableA
where ( City_Code == r.City_Code )
select r ).ToList();
while this code will return quickly
String City_Code = null;
var result = (from r in myContext.TableA
where ( r.City_Code == City_Code )
select r ).ToList();
The difference is in the order of operands of the equality. The field City_Code of table TableA is of type nvarchar(20).
What is a possible reason for that???
update: The generated tsql for the 2 queries are identical except for that part: the first one has the condition " #p__linq__2 = [Extent1].[City_Code])" while the second has " [Extent1].[City_Code] = #p__linq__2)"
The value of #p_linq_2 when I notice the time difference is NULL. If I run the queries in ssms with NULL in place of #p_linq_2 they respond both very quickly
I am trying to build a BQL query using the same table more than once. In my example below I am trying to use POVendorInventory as it relates to the default for the item and for each item warehouse (reason for the table listed twice in the query).
How do I do this in an Acumatica BQL Query. My current query is:
foreach (PXResult<INItemSite, InventoryItem, POVendorInventory, POVendorInventory> result in PXSelectJoin<INItemSite,
InnerJoin<InventoryItem, On<INItemSite.inventoryID, Equal<InventoryItem.inventoryID>>,
LeftJoin<POVendorInventory, On<InventoryItem.inventoryID, Equal<POVendorInventory.inventoryID>
, And<InventoryItem.preferredVendorID, Equal<POVendorInventory.vendorID>
, And<InventoryItem.preferredVendorLocationID, Equal<POVendorInventory.vendorLocationID>>>>,
LeftJoin<POVendorInventory, On<INItemSite.preferredVendorID, Equal<POVendorInventory.vendorID>,
And<INItemSite.inventoryID, Equal<POVendorInventory.inventoryID>>>>>>,
Where<InventoryItem.stkItem, Equal<boolTrue>>,
OrderBy<Asc<INItemSite.inventoryID, Asc<INItemSite.siteID>>>>.Select(graph))
{
var poVendorInventoryDefault = (POVendorInventory)result[2];
var poVendorInventorySite = (POVendorInventory)result[3];
}
The above results in the following error when the query is executed:
The correlation name 'POVendorInventory' is specified multiple times in a FROM clause.
The similar query in MS SQL is:
SELECT s.InventoryID ,
i.InventoryCD ,
s.SiteID ,
n.SiteCD ,
s.PreferredVendorID ,
s.PreferredVendorOverride ,
pd.VendorID ,
pd.AddLeadTimeDays ,
ps.VendorID ,
ps.AddLeadTimeDays
FROM dbo.INItemSite s
INNER JOIN dbo.InventoryItem i ON s.CompanyID = i.CompanyID
AND s.InventoryID = i.InventoryID
INNER JOIN dbo.INSite n ON s.CompanyID = n.CompanyID
AND s.SiteID = n.SiteID
LEFT JOIN dbo.POVendorInventory pd ON i.CompanyID = pd.CompanyID
AND i.InventoryID = pd.InventoryID
AND i.PreferredVendorID = pd.VendorID
AND i.PreferredVendorLocationID = pd.VendorLocationID
LEFT JOIN dbo.POVendorInventory ps ON s.CompanyID = ps.CompanyID
AND s.InventoryID = ps.InventoryID
AND s.PreferredVendorID = ps.VendorID
WHERE s.CompanyID = 2
AND i.StkItem = 1
ORDER BY s.InventoryID, s.SiteID
The following does not work either.
[Serializable]
public class POVendorInventoryTwo : POVendorInventory{}
PXResult<INItemSite, InventoryItem, POVendorInventoryTwo, POVendorInventory> result in PXSelectJoin<INItemSite,
InnerJoin<InventoryItem, On<INItemSite.inventoryID, Equal<InventoryItem.inventoryID>>,
LeftJoin<POVendorInventoryTwo, On<InventoryItem.inventoryID, Equal<POVendorInventoryTwo.inventoryID>
, And<InventoryItem.preferredVendorID, Equal<POVendorInventoryTwo.vendorID>
, And<InventoryItem.preferredVendorLocationID, Equal<POVendorInventoryTwo.vendorLocationID>>>>,
LeftJoin<POVendorInventory, On<INItemSite.preferredVendorID, Equal<POVendorInventory.vendorID>,
And<INItemSite.inventoryID, Equal<POVendorInventory.inventoryID>>>>>>,
Where<InventoryItem.stkItem, Equal<boolTrue>>,
OrderBy<Asc<INItemSite.inventoryID, Asc<INItemSite.siteID>>>>.Select(graph)
But as the answer mentions you need to use POVendorInventoryTwo for the second usage like so:
PXResult<INItemSite, InventoryItem, POVendorInventory, POVendorInventoryTwo> result in PXSelectJoin<INItemSite,
InnerJoin<InventoryItem, On<INItemSite.inventoryID, Equal<InventoryItem.inventoryID>>,
LeftJoin<POVendorInventory, On<InventoryItem.inventoryID, Equal<POVendorInventory.inventoryID>
, And<InventoryItem.preferredVendorID, Equal<POVendorInventory.vendorID>
, And<InventoryItem.preferredVendorLocationID, Equal<POVendorInventory.vendorLocationID>>>>,
LeftJoin<POVendorInventoryTwo, On<INItemSite.preferredVendorID, Equal<POVendorInventoryTwo.vendorID>,
And<INItemSite.inventoryID, Equal<POVendorInventoryTwo.inventoryID>>>>>>,
Where<InventoryItem.stkItem, Equal<boolTrue>>,
OrderBy<Asc<INItemSite.inventoryID, Asc<INItemSite.siteID>>>>.Select(graph)
What I've done in the past is subclass the DAC and query off that.
Something like this:
public class POVendorInventoryTwo : POVendorInventory
{}
Then in your BQL, use this subclassed version for the second value.