I have a table
CREATE TABLE [dbo].[DocGenVariable](
[userkey_sessionid] [varchar](38) NOT NULL,
[is_session] [bit] NULL,
[var_name] [nvarchar](255) NOT NULL,
[var_value] [nvarchar](1000) NOT NULL,
[topic_id] [varchar](38) NOT NULL,
[resource_id] [int] NULL,
[added] [datetime] NOT NULL,
CONSTRAINT [PK_DocGenVariable] PRIMARY KEY CLUSTERED
(
[userkey_sessionid] ASC,
[var_name] ASC,
[topic_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
Sometimes (attention > sometimes) I get an error
"Violation of PRIMARY KEY constraint 'PK_DocGenVariable'.
Cannot insert duplicate key in object 'dbo.DocGenVariable'.
when I try to add an entry into this table
delete from DocGenVariable where userkey_sessionid = #key_session and var_name = #var_name and topic_id = #topic_id
if (LTRIM(RTRIM(#var_value)) <> '')
begin
insert into DocGenVariable(userkey_sessionid, is_session, var_name, var_value, topic_id, resource_id, added)
values (#key_session, #is_session, #var_name, #var_value, #topic_id, #resource_id, GetDate())
end
What I do first is deleting an entry which must be updated, then add a new entry.
Why do I get this constraint error?
Why delete then insert? Just update it.
IF EXISTS(select 1 from DocGenVariable where userkey_sessionid = #key_session
and
var_name = #var_name and topic_id = #topic_id)
BEGIN
UPDATE ....
END ELSE BEGIN
INSERT ...
END
Try upsert
using DocGenVariable target
using DocGenVariable source
on source.userkey_sessionid = #key_session
and source.var_name = #var_name
and source.topic_id = #topic_id
and target.userkey_sessionid = #key_session
and target.var_name = #var_name
and target.topic_id = #topic_id
when match
set target.resource_id = #resource_id
, target .added = GetDate()
when not matched then
insert into DocGenVariable(userkey_sessionid, is_session, var_name, var_value, topic_id, resource_id, added)
values (#key_session, #is_session, #var_name, #var_value, #topic_id, #resource_id, GetDate())
Related
CREATE TABLE [dbo].[USERS]
(
[USER_NAME] [nvarchar](64) NOT NULL,
[EMAIL_ADDRESS] [nvarchar](256) NULL,
[REGISTERED_DATE] [datetime] NOT NULL,
[FULL_NAME] [nvarchar](256) NOT NULL,
[MANAGER] [nvarchar](64) NULL,
[DEPARTMENT] [nvarchar](256) NULL,
[TITLE] [nvarchar](64) NULL,
PRIMARY KEY CLUSTERED ([USER_NAME] ASC)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
This is my table and I want the trigger to fire when the manager column is updated and the same values of the manage have to update on the another table based on the USER_NAME column.
i tried the the below trigger but it is not working.
Create TRIGGER [dbo].[UpdateManager]
ON [dbo].[USERS]
AFTER UPDATE
AS
BEGIN
SET NOCOUNT ON;
declare #USER_NAME as nvarchar
declare #MANAGER as nvarchar
if(update(MANAGER))
begin
select #USER_NAME = USER_NAME from deleted
select #manager = Manager from deleted
update [dbo].[USERS_TEST] set MANAGER=#MANAGER where USER_NAME= #USER_NAME
end
END
You have two MAJOR issues in your trigger:
As mentioned in a comment, deleted can have multiple rows and you don't take that into account.
nvarchar has no length; the default in this case is a length of 1.
I suspect that the query you want is:
update ut
set MANAGER = d.MANAGER
from [dbo].[USERS_TEST] ut join
deleted d
on ut.USER_NAME = d.USER_NAME ;
I am building a pretty simple inventory items database that will allow me to check out items between dates. I will need to return a single row that will tell me the available, reserved, and total inventory of this item. I cannot seem to get this correct.
BEGIN TRAN
DECLARE #startDate AS DATE
DECLARE #endDate AS DATE
DECLARE #partID AS INT
SET #startDate = '4/15/2015'
SET #endDate = '4/25/2015'
SET #partID = 248
SELECT COUNT(ii.[PartId] WHERE ii.[PartStatus] = 1 ) AS [Available],
COUNT(ii.[PartId] WHERE ii.[PartStatus] = 2 ) AS [Reserverd],
COUNT(ii.[PartId] WHERE ii.[PartStatus] <> 4 ) AS [TotalInventory],
FROM ShipListInventory.dbo.InventoryItems AS ii
LEFT JOIN ShipListInventory.dbo.InventoryItemCalendars AS iic
ON iic.[ItemId] = ii.[Id]
WHERE iic.[StartDate] NOT BETWEEN #startDate AND #endDate
AND iic.[InboundDate] NOT BETWEEN #startDate AND #endDate
AND ii.[PartId] = 248
COMMIT TRAN
EDIT: I am only returning rows from InventoryItemCalendarsI want to return items from InventoryItems
CREATE TABLE [dbo].[InventoryItems](
[Id] [INT] IDENTITY(1,1) NOT NULL,
[PartSatuts] [INT] NOT NULL,
[PartId] [INT] NOT NULL,
[Barcode] [NVARCHAR](MAX) NULL,
[PicturePath] [NVARCHAR](255) NULL,
[Notes] [NVARCHAR](1000) NULL,
CONSTRAINT [PK_dbo.InventoryItems] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
ALTER TABLE [dbo].[InventoryItems] WITH CHECK ADD CONSTRAINT [FK_dbo.InventoryItems_dbo.Parts_PartId] FOREIGN KEY([PartId])
REFERENCES [dbo].[Parts] ([Id])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[InventoryItems] CHECK CONSTRAINT [FK_dbo.InventoryItems_dbo.Parts_PartId]
GO
CREATE TABLE [dbo].[InventoryItemCalendars](
[Id] [INT] IDENTITY(1,1) NOT NULL,
[StartDate] [DATETIME] NOT NULL,
[InboundDate] [DATETIME] NOT NULL,
[ProjectNumber] [INT] NOT NULL,
[ItemId] [INT] NOT NULL,
[Project_Id] [INT] NULL,
CONSTRAINT [PK_dbo.InventoryItemCalendars] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[InventoryItemCalendars] WITH CHECK ADD CONSTRAINT [FK_dbo.InventoryItemCalendars_dbo.InventoryItems_ItemId] FOREIGN KEY([ItemId])
REFERENCES [dbo].[InventoryItems] ([Id])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[InventoryItemCalendars] CHECK CONSTRAINT [FK_dbo.InventoryItemCalendars_dbo.InventoryItems_ItemId]
GO
ALTER TABLE [dbo].[InventoryItemCalendars] WITH CHECK ADD CONSTRAINT [FK_dbo.InventoryItemCalendars_dbo.Projects_Project_Id] FOREIGN KEY([Project_Id])
REFERENCES [dbo].[Projects] ([Id])
GO
ALTER TABLE [dbo].[InventoryItemCalendars] CHECK CONSTRAINT [FK_dbo.InventoryItemCalendars_dbo.Projects_Project_Id]
GO
INSERT INTO ShipListInventory.[dbo].[InventoryItems]([PartSatuts],[PartId]) VALUES(1,1);
INSERT INTO ShipListInventory.[dbo].[InventoryItems]([PartSatuts],[PartId]) VALUES(2,1);
INSERT INTO ShipListInventory.[dbo].[InventoryItems]([PartSatuts],[PartId]) VALUES(1,1);
INSERT INTO ShipListInventory.[dbo].[InventoryItems]([PartSatuts],[PartId]) VALUES(4,1);
INSERT INTO ShipListInventory.[dbo].[InventoryItemCalendars](StartDate,InboundDate,Project_Id, ItemId)VALUES('1/1/2015', '4/15/2015',1,1)
INSERT INTO ShipListInventory.[dbo].[InventoryItemCalendars](StartDate,InboundDate,Project_Id, ItemId)VALUES('1/1/2015', '4/15/2015',1,1)
INSERT INTO ShipListInventory.[dbo].[InventoryItemCalendars](StartDate,InboundDate,Project_Id, ItemId)VALUES('1/1/2015', '4/14/2015',1,2)
INSERT INTO ShipListInventory.[dbo].[InventoryItemCalendars](StartDate,InboundDate,Project_Id, ItemId)VALUES('1/1/2015', '4/15/2015',1,1)
this isnt returning data from correct table it is only returning data form InventoryItemCalendars which isnt what i want. i want it to only use this to check the dates. it should return the items from InventoryItems table
Here is a syntactically correct version:
SELECT SUM(CASE WHEN ii.[PartStatus] = 1 THEN 1 ELSE 0 END) AS [Available],
SUM(CASE WHEN ii.[PartStatus] = 2 THEN 1 ELSE 0 END) AS [Reserverd],
SUM(CASE WHEN ii.[PartStatus] <> 4 THEN 1 ELSE 0 END) AS [TotalInventory]
FROM ShipListInventory.dbo.InventoryItems ii LEFT JOIN
ShipListInventory.dbo.InventoryItemCalendars AS iic
ON iic.[ItemId] = ii.[Id]
WHERE iic.[StartDate] NOT BETWEEN #startDate AND #endDate AND
iic.[InboundDate] NOT BETWEEN #startDate AND #endDate AND
ii.[PartId] = 248;
There is no transaction when you do a select, so committing is not necessary.
SELECT COUNT(CASE WHEN ii.[PartStatus] = 1 THEN ii.[PartId] END) AS [Available],
COUNT(CASE WHEN ii.[PartStatus] = 2 THEN ii.[PartId] END) AS [Reserverd],
COUNT(CASE WHEN ii.[PartStatus] <> 4 THEN ii.[PartId] END) AS [TotalInventory]
With the help of Juan Carlos I have found the correct answer.
SELECT COUNT(CASE WHEN ii.[PartSatuts]=1 THEN ii.[PartId] END ) AS [Available],
COUNT(CASE WHEN ii.[PartSatuts]=2 THEN ii.[PartId] END ) AS [Reserverd],
COUNT(CASE WHEN ii.[PartSatuts]<> 4 THEN ii.[PartId] END ) AS [TotalInventory]
FROM ShipListInventory.dbo.InventoryItems AS ii
LEFT JOIN ShipListInventory.dbo.InventoryItemCalendars AS iic
ON iic.ItemId = ii.Id
WHERE ((iic.StartDate NOT BETWEEN #startDate AND #endDate
AND iic.InboundDate NOT BETWEEN #startDate AND #endDate)
OR iic.InboundDate IS NULL)
AND ii.PartId = 248
I have a table UserInfoComputed whose data comes from UserInfo and UserInfoComputed also have foreign key constraint to UserCompany.
I have a trigger on UserInfo which inserts/updates/deletes rows into UserInfoComputed
Here is my UserInfoComputed table:
CREATE TABLE [dbo].[UserInfoComputed ](
[id] AS (CONVERT([bigint],replace([law_id],'LAW',''),(0))) PERSISTED NOT NULL,
[company_id] [varchar](12) NOT NULL,
[first_name] [varchar](30) NOT NULL,
[last_name] [varchar](30) NOT NULL,
[law_id] [varchar](12) NOT NULL,
[type] [smallint] NULL,
[dtype] AS (case [TYPE] when (1) then 'Corporate' else 'Non-Corporate' end) PERSISTED NOT NULL,
CONSTRAINT [PK_UserInfoComputed] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[UserInfoComputed] WITH CHECK ADD CONSTRAINT [FK668581C04AA07B12] FOREIGN KEY([company_id])
REFERENCES [dbo].[UserCompany] ([id])
GO
Here is my UserInfo table
CREATE TABLE [dbo].[UserInfo](
[ID] [varchar](12) NOT NULL,
[CompanyID] [varchar](12) NULL,
[Status] [char](4) NULL,
[FirstName] [varchar](30) NOT NULL,
[LastName] [varchar](30) NOT NULL,
CONSTRAINT [PK_UserInfo] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
Here is the UserCompany table
CREATE TABLE [dbo].[UserCompany](
[ID] [varchar](12) NOT NULL,
[Name] [varchar](100) NULL,
[ShortName] [varchar](25) NULL,
[Type] [smallint] NULL,
CONSTRAINT [PK_UserCompany] PRIMARY KEY NONCLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
The type values are either 1 or 2 or 3
Here is my trigger on UserInfo table
CREATE TRIGGER [dbo].[ReconcileUserInfoComputed]
ON [dbo].[UserInformation]
AFTER INSERT,DELETE,UPDATE
AS
IF ##ROWCOUNT = 0 -- exit trigger when zero records affected
BEGIN
RETURN
END
IF EXISTS (SELECT * FROM INSERTED)
BEGIN
IF EXISTS (SELECT * FROM DELETED)
BEGIN
--UPDATE
UPDATE [dbo].[UserInformationComputed]
SET -- use new values from inserted
first_name = (SELECT FirstName FROM inserted),
last_name = (SELECT LastName FROM inserted),
law_id = (SELECT ID FROM inserted)
WHERE law_id = (SELECT ID FROM deleted)
END
ELSE
BEGIN
--INSERT
INSERT INTO [dbo].[UserInfoComputed] (first_name,last_name, law_id)
SELECT FirstName, LastName, ID FROM inserted
END
END
ELSE IF EXISTS(SELECT * FROM DELETED)
BEGIN
--DELETE
DELETE FROM [dbo].[UserInfoComputed]
WHERE law_id = (SELECT id FROM deleted)
END
GO
Is there a way to insert or update type value from UserCompany into UserInfoComputed table in ReconcileUserInfoComputed trigger?
I have a simple database table (SQL Server 2008 R2 Express), which has a definition as follows:
ID INT Auto Inc, PK
Name VARCHAR(64) Unique Key
Telephone VARCHAR(128)
I have a stored procedure which I execute to update records within the table which basically does the following:
UPDATE customers
SET Name = #name, Telephone = #Telephone
WHERE id = #id
Currently, I have two entries in the table
ID Name Telephone
1 Fred 01234 567890
2 John 09876 543210
When I call my stored procedure to update the telephone number for John, the SQL that is effectively executed is
UPDATE customers
SET Name = 'John', Telephone = '02468 135790'
WHERE id = 2
This generates a UNIQUE KEY violation on the Name field. Now as the Name field doesn't actually change, why does this occur?
As all database actions are being handled by my app using stored procedures, I could fix this by removing the constraint, and modifying the stored procedures to manually enforce the constraint, but this just seems wrong.
Given that my table actually has many more fields, there must be a generic work around that I can employ to prevent these false constraint problems, without having to generate numerous stored procedures to update specific fields?
Edit: The above table was simplified to keep the question more manageable, I'm pretty sure I've not missed anything important, but for info, the actual definition of the table is as follows
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[companies](
[id] [int] IDENTITY(1,1) NOT NULL,
[typeId] [int] NOT NULL,
[name] [varchar](64) NOT NULL,
[displayName] [varchar](128) NOT NULL,
[deliveryAddress] [varchar](1024) NOT NULL,
[invoiceAddress] [varchar](1024) NOT NULL,
[telephone] [varchar](64) NOT NULL,
[fax] [varchar](64) NOT NULL,
[email] [varchar](256) NOT NULL,
[website] [varchar](256) NULL,
[isActive] [bit] NOT NULL,
CONSTRAINT [PK_companies] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
CONSTRAINT [Unique Display Name] UNIQUE NONCLUSTERED
(
[displayName] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
CONSTRAINT [Unique Name] UNIQUE NONCLUSTERED
(
[name] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[companies] WITH CHECK ADD CONSTRAINT [Company Type] FOREIGN KEY([id])
REFERENCES [dbo].[companyTypes] ([id])
GO
ALTER TABLE [dbo].[companies] CHECK CONSTRAINT [Company Type]
GO
...and the stored procedure
ALTER PROCEDURE UpdateCompany
#id INT,
#typeId INT,
#name VARCHAR(64),
#displayName VARCHAR(128),
#deliveryAddress VARCHAR(1024),
#invoiceAddress VARCHAR(1024),
#telephone VARCHAR(64),
#fax VARCHAR(64),
#email VARCHAR(256),
#website VARCHAR(256),
#isActive BIT
AS
BEGIN
UPDATE companies
SET typeid = #typeid,
name = #name,
displayname = #displayname,
deliveryAddress = #deliveryAddress,
invoiceAddress = #invoiceAddress,
telephone = #telephone,
fax = #fax,
email = #email,
website = #website,
isActive = #isActive
EXEC GetCompany #id
END
GO
You're missing the WHERE in your UPDATE statement so currently it will try and update all rows in the table with the same values.
I wish to migrate some data from a single table into these new THREE tables.
Here's my destination schema:
Notice that I need to insert into the first Location table .. grab the SCOPE_IDENTITY() .. then insert the rows into the Boundary and Country tables.
The SCOPE_IDENTITY() is killing me :( meaning, I can only see a way to do this via CURSORS. Is there a better alternative?
UPDATE
Here's the scripts for the DB Schema....
Location
CREATE TABLE [dbo].[Locations](
[LocationId] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](100) NOT NULL,
[OriginalLocationId] [int] NOT NULL,
CONSTRAINT [PK_Locations] PRIMARY KEY CLUSTERED
(
[LocationId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
)
Country
CREATE TABLE [dbo].[Locations_Country](
[IsoCode] [nchar](2) NOT NULL,
[LocationId] [int] NOT NULL,
CONSTRAINT [PK_Locations_Country] PRIMARY KEY CLUSTERED
(
[LocationId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Locations_Country] WITH CHECK ADD CONSTRAINT [FK_Country_inherits_Location] FOREIGN KEY([LocationId])
REFERENCES [dbo].[Locations] ([LocationId])
GO
ALTER TABLE [dbo].[Locations_Country] CHECK CONSTRAINT [FK_Country_inherits_Location]
GO
Boundary
CREATE TABLE [dbo].[Boundaries](
[LocationId] [int] NOT NULL,
[CentrePoint] [varbinary](max) NOT NULL,
[OriginalBoundary] [varbinary](max) NULL,
[LargeReducedBoundary] [varbinary](max) NULL,
[MediumReducedBoundary] [varbinary](max) NULL,
[SmallReducedBoundary] [varbinary](max) NULL,
CONSTRAINT [PK_Boundaries] PRIMARY KEY CLUSTERED
(
[LocationId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[Boundaries] WITH CHECK ADD CONSTRAINT [FK_LocationBoundary] FOREIGN KEY([LocationId])
REFERENCES [dbo].[Locations] ([LocationId])
GO
ALTER TABLE [dbo].[Boundaries] CHECK CONSTRAINT [FK_LocationBoundary]
GO
I don't see a need for SCOPE_IDENTITY or cursors if you approach the data in order of the parent/child relationship:
INSERT INTO LOCATION
SELECT t.name,
t.originallocationid
FROM ORIGINAL_TABLE t
GROUP BY t.name, t.originallocationid
INSERT INTO COUNTRY
SELECT DISTINCT
t.isocode,
l.locationid
FROM ORIGINAL_TABLE t
JOIN LOCATION l ON l.name = t.name
AND l.originallocationid = t.originalocationid
INSERT INTO BOUNDARY
SELECT DISTINCT
l.locationid,
t.centrepoint,
t.originalboundary,
t.largereducedboundary,
t.mediumreducedboundary,
t.smallreducedboundary
FROM ORIGINAL_TABLE t
JOIN LOCATION l ON l.name = t.name
AND l.originallocationid = t.originalocationid
After loading your Location table you could create a query that joins Location with your source single table. The join criteria would be the natural key (is that the Name column?) and it would return the new LocationId along with the Boundary data. The results would inserted into the new Boundary table.