Trigger not finding table - sql

I'm learning about and how to use SQL triggers. I'm building a test database that isn't part of my classes so I can practice.
The problem is I'm getting
The object 'dbo.StudentInfo' does not exist or is invalid for this
operation
1) I have permissions, I'm the administrator.
2) I'm absolutely positive I'm in the correct database.
3) I have tried dbo.StudentInfo, StudentInfo, [dbo.StudentInfo] (which I know is wrong anyway) and [dbo].[StudentInfo], and all of them give me the same error.
What else could be wrong? What other information would you need to help?
Use Test3
GO
CREATE TRIGGER tr_high_date
ON [dbo].[StudentInfo]
FOR INSERT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for trigger here
END
GO
Table Information:
USE [test3]
GO
/****** Object: Table [dbo].[StudentInfo] Script Date: 9/22/2015 9:39:53 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[StudentInfo](
[StudentID] [char](8) NOT NULL,
[LastName] [varchar](20) NOT NULL,
[FirstName] [varchar](20) NOT NULL,
[NickName] [varchar](20) NOT NULL,
[PhoneNumber] [char](10) NULL,
[Email] [varchar](40) NOT NULL,
[DateAdded] [date] NOT NULL,
[DateExpectedGraduation] [date] NOT NULL,
CONSTRAINT [PK_StudentInfo] PRIMARY KEY CLUSTERED
(
[StudentID] 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

Are you sure you are using the correct database? Maybe you're in the master database and your tables are in a different database or vice versa.

Related

Unique constraint on another column if not null

I want to create this table with the following constrains, is it possible to do with SQL Server Management Studio?
Id
ProductId [Nullable column]
CompanyId [Nullable column]
Username [unique for every productId - IF DeletedAt is not NULL]
Identifier [Nullable column] [unique for every companyId - IF DeletedAt is not NULL]
DeletedAt [Nullable column]
Update:
My table create query is the following:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[User](
[Id] [int] IDENTITY(1,1) NOT NULL,
[ProductId] [int] NULL,
[CompanyId] [int] NULL,
[Username] [nvarchar](max) NOT NULL,
[Identifier] [nvarchar](max) NULL,
[DeletedAt] [datetime2](7) NULL,
CONSTRAINT [PK_User] 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
You'll want to use a unique filtered index. Here you go for the table:
/****** Object: Table [dbo].[the_table] Script Date: 7/26/2018 4:04:00 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[the_table](
[id] [int] NOT NULL,
[productid] [varchar](50) NULL,
[companyid] [varchar](50) NULL,
[username] [varchar](50) NULL,
[identifier] [varchar](50) NULL,
[deleteat] [varchar](50) NULL,
CONSTRAINT [PK_the_table] 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
And the index for username:
SET ANSI_PADDING ON
GO
/****** Object: Index [UIDX_USERNAME] Script Date: 7/26/2018 4:03:31 PM ******/
CREATE NONCLUSTERED INDEX [UIDX_USERNAME] ON [dbo].[the_table]
(
[username] ASC
)
WHERE ([DELETEAT] IS NOT NULL)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
It would be the same index logic for the other column, but I don't want to clutter this answer by posting more! I would not mess around with putting the logic in triggers or stored proc's, bad data could still sneak in. If you keep nice constraints, nobody can clutter your data up :)
You can use filtered indexes. The syntax looks like this:
create unique index unq_thetable_username on the_table(username)
where deleteAt is not null;
I don't know if you can point-and-click your way to this solution. I would just write the logic as above.
Create a unique index on ProductId and Username, and use a where clause to restrict that index to WHERE DeletedAt IS NOT NULL. Answer showing an example of a unique filtered index.
You may also need the clause AND ProductId IS NOT NULL, although that wasn't stated as a requirement, but since that column is nullable you ought to think about this.
Similar for Identifier.
You might also want to think about the value of a table where it is valid to have a row where all the columns except Id are null...

Adding a SQL Default value of 01 to a database column

How do you add the following default value to a sql database table?
01
I have tried various datatypes but the 0 keeps disappearing. I haven't even been able to find an example on the exchange or google but I assume this is very straight forward and I am being a numpty!!!
I have tried numeric/integer/varchar without luck. Thanks.
create table MyTable (Id int, Value varchar(100) not null default('01'))
Insert into MyTable (Id) values (1)
Select Value
From MyTable
will be '01'
Sql fiddle
Another option could be:
Open your Table Designer and you will see something like this:
If you prefer code:
USE ["name of your dataBase"]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Table_1](
[id] [int] IDENTITY(1,1) NOT NULL,
[sample02] [nvarchar](50) NOT NULL,
[sample03] [nvarchar](50) NULL,
CONSTRAINT [PK_Table_1] 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].[Table_1] ADD CONSTRAINT [DF_Table_1_sample02] DEFAULT ('01') FOR [sample02]
GO
And finally, you can visit this Microsoft post: Specify Default Values for Columns

SQL Server : complicated inserts

I have two tables, which I want to fill with data. Those tables are Threads and Posts. Also I have a table called Source which contains data.
Threads and Posts contain a lot of columns to be filled, so I will not paste them here for the sake of simplicity, but most of them can be some fixed value. Source table contains following columns - title (goes into Threads.title), postContent (goes into Posts.content)
In order to copy data:
I need to copy title column from Source table into Threads table, and add some fixed date, and author username into it (I want author to be some constant string, and date to be autogenerated DateTime from some T-SQL function)
Now when the Threads row is created, I need to get it's ID, and create new Posts row, which will contain ID of new thread, content from Source.postContent, and some other fixed values
I know that this probably is complicated, but can you maybe give me some guidelines here? How do I do such a thing? The main issue here, is the need of creating Threads first, and then using it's ID in Posts.
Please try with the below code snippet.
Create Table and add dummy data
/****** Object: Table [dbo].[Threads] Script Date: 11/06/2013 13:57:51 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Threads](
[ThreadID] [int] IDENTITY(1,1) NOT NULL,
[ThreadTitle] [nvarchar](50) NOT NULL,
CONSTRAINT [PK_Threads] PRIMARY KEY CLUSTERED
(
[ThreadID] 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
/****** Object: Table [dbo].[SourceTable] Script Date: 11/06/2013 13:57:51 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[SourceTable](
[SourceTableID] [int] IDENTITY(1,1) NOT NULL,
[SourceTitle] [nvarchar](50) NULL,
[SourceContent] [nvarchar](50) NULL,
CONSTRAINT [PK_SourceTable] PRIMARY KEY CLUSTERED
(
[SourceTableID] 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
/****** Object: Table [dbo].[Posts] Script Date: 11/06/2013 13:57:51 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Posts](
[PostID] [int] IDENTITY(1,1) NOT NULL,
[PostContent] [nvarchar](50) NULL,
[ThreadID] [int] NOT NULL,
CONSTRAINT [PK_Posts] PRIMARY KEY CLUSTERED
(
[PostID] 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
/****** Object: ForeignKey [FK_Posts_Threads] Script Date: 11/06/2013 13:57:51 ******/
ALTER TABLE [dbo].[Posts] WITH CHECK ADD CONSTRAINT [FK_Posts_Threads] FOREIGN KEY([ThreadID])
REFERENCES [dbo].[Threads] ([ThreadID])
GO
ALTER TABLE [dbo].[Posts] CHECK CONSTRAINT [FK_Posts_Threads]
GO
SET IDENTITY_INSERT [dbo].[SourceTable] ON
INSERT [dbo].[SourceTable] ([SourceTableID], [SourceTitle], [SourceContent]) VALUES (1, N'blog1', N'blogdesc1')
INSERT [dbo].[SourceTable] ([SourceTableID], [SourceTitle], [SourceContent]) VALUES (2, N'blog2', N'blogdesc2')
INSERT [dbo].[SourceTable] ([SourceTableID], [SourceTitle], [SourceContent]) VALUES (3, N'blog3', N'blogdesc3')
SET IDENTITY_INSERT [dbo].[SourceTable] OFF
Query to insert data in table
CREATE TABLE #SummaryOfChanges(actionType NVARCHAR(50),ThreadID NVARCHAR(40),SourceContent NVARCHAR(40))
MERGE INTO Threads AS d
USING (SELECT SourceTableID,SourceTitle,SourceContent FROM SourceTable) AS s
ON 1 = 2
WHEN NOT MATCHED THEN
INSERT (ThreadTitle)
VALUES (s.SourceTitle)
OUTPUT $action, Inserted.ThreadID, s.SourceContent INTO #SummaryOfChanges;
MERGE INTO Posts AS d
USING (SELECT ThreadID,SourceContent FROM #SummaryOfChanges) AS s
ON d.ThreadID = s.ThreadID
WHEN MATCHED THEN
UPDATE SET d.PostContent= s.SourceContent
WHEN NOT MATCHED THEN
INSERT (ThreadID,PostContent)
VALUES (ThreadID,s.SourceContent);
DROP TABLE #SummaryOfChanges
Let me know if any concern.

A dash (-) in a char data type?

So I'm trying to insert data into a table in my database that has a PK column that has a data type of char. There are currently records in that column that are formatted like this 27-5. However, I cannot seem to think of a way to replicate this with an insert statement.
My current insert statement obviously just performs the concatenation manually:
Insert into kc.ep_act_steps Values (1,1-3,'Some Example Text',NULL,3,1)
That column happens to be the creation of the last two columns in reverse order. How can I achieve this?
Edit: Updated with table definition (removed some unneccessary key stuff)
USE [ICKC_2]
GO
/****** Object: Table [kc].[ep_act_steps] Script Date: 06/18/2013 17:27:35 ******/
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
SET ANSI_PADDING ON
CREATE TABLE [kc].[ep_act_steps](
[act_type] [char](16) NOT NULL,
[act_id] [char](16) NOT NULL,
[description] [varchar](2000) NULL,
[ep_procedure] [int] NULL,
[display_order] [smallint] NULL,
[org_id] [int] NULL,
CONSTRAINT [PK__ep_act_steps__22751F6C] PRIMARY KEY CLUSTERED
(
[act_type] ASC,
[act_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
Edit: Cannot enclose in apostrophes despite having all PK constraints turned off using sp_MSForEachTable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'. Using apostrophes results in the following error: `Violation of PRIMARY KEY constraint 'PK__ep_act_steps__22751F6C'. Cannot insert duplicate key in object 'kc.ep_act_steps'.'
Insert into kc.ep_act_steps Values ('1','1-3','Some Example Text',NULL,3,1)

SQL Data entities Entity Association

I have about 8 entities that all have a one to one relationship with a common entity.
The client is not choosing from pre-defined data so it is not necessary to apply a FK to be used as a constraint.
The main table in question is call a finish table and it contains four unknown hex colors that are sent from the client to the server.
For example, when a door is built the colors for the different parts of the door and it's outer parts can all have different colors. So, all of these tables in question, their data is always fresh from the client and not chosen from a drop down, or from some other pre-defined data that I am given to the client to choose from.
My question is, what would be the best way to association this finish entity with the other entities that need a way to express their finish?
I am adding a screen shot of a diagram that I am working on, these are not all the entities and are just the ones in question right now and ones that will help articulate to others what I am needing a solution for.
I have also included some script as well.
/****** Object: Table [dbo].[Finish] Script Date: 06/22/2012 15:08:37 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Finish](
[ID] [int] NOT NULL,
[Left] [varchar](30) NULL,
[Right] [varchar](30) NULL,
[Top] [varchar](30) NULL,
[Bottom] [varchar](30) NULL,
[Note] [varchar](150) NULL,
CONSTRAINT [PK_Finish] 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
SET ANSI_PADDING OFF
GO
/****** Object: Table [dbo].[Horizontal] Script Date: 06/22/2012 15:08:37 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Horizontal](
[ID] [int] IDENTITY(11,1) NOT NULL,
[Name] [varchar](15) NOT NULL,
[Floor] [smallint] NOT NULL,
[SizeID] [int] NOT NULL,
[GlassPocket] [decimal](5, 3) NULL,
[IsFiller] [bit] NOT NULL,
[Note] [varchar](150) NULL,
CONSTRAINT [PK_Horizontal] 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
SET ANSI_PADDING OFF
GO
/****** Object: Table [dbo].[Door] Script Date: 06/22/2012 15:08:36 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Door](
[ID] [int] IDENTITY(421,1) NOT NULL,
[BayID] [int] NOT NULL,
[Position] [tinyint] NOT NULL,
[HasJamb] [bit] NOT NULL,
[HasThreshold] [bit] NOT NULL,
[IsAutoShowroom] [bit] NOT NULL,
[IsSingle] [bit] NOT NULL,
[Type] [varchar](10) NOT NULL,
[SizeID] [int] NOT NULL,
[Note] [varchar](150) NULL,
CONSTRAINT [PK_Door] 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
SET ANSI_PADDING OFF
GO
/****** Object: Table [dbo].[Leaf] Script Date: 06/22/2012 15:08:37 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Leaf](
[ID] [int] IDENTITY(21,1) NOT NULL,
[DoorID] [int] NOT NULL,
[Position] [tinyint] NOT NULL,
[Stile] [varchar](10) NOT NULL,
[Bottomrail] [decimal](5, 3) NOT NULL,
[Hand] [varchar](5) NOT NULL,
[IsActive] [bit] NOT NULL,
[Swing] [varchar](5) NOT NULL,
[SizeID] [int] NOT NULL,
[Note] [varchar](150) NULL,
CONSTRAINT [PK_Leaf] 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
SET ANSI_PADDING OFF
GO
/****** Object: Table [dbo].[Bay] Script Date: 06/22/2012 15:08:36 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Bay](
[ID] [int] IDENTITY(1213,1) NOT NULL,
[ElevationID] [int] NOT NULL,
[Position] [tinyint] NOT NULL,
[SizeID] [int] NOT NULL,
[Note] [varchar](150) NULL,
CONSTRAINT [PK_Bay] 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
SET ANSI_PADDING OFF
GO
Each one of the entities below need to be associated with the finish table. Each entity new record has exactly one finish relationship.
Is it possible to associate these and still be able to do a Cascade On DELETE, with-out having a circular reference issues?
Yes, it's possible.
You would add a reference to Finish entity in each of the tables that has a relationship to it.
You would define the column with the same datatype, (and normally) as the referenced_table_name and id. In our shop, the column_name would be [finish_id]. (I see here that you are using a CamelCase style.)
At any rate, I would recommend you define this as a foreign key.
You say there is no need to, but from what you describe, this is exactly the kind of situation that calls for a foreign key constraint.
You need to decide on the action when the id in the finish table is updated or deleted. (Do you want to disallow the update or delete? Do you want to preserve existing relationship?) I expect you would want the default ON DELETE RESTRICT. You could allow for updates, and preserve the relationships, with ON UPDATE CASCADE.)
I don't see any potential problem with circular references, as long as the finish table will be the parent (all the other tables reference it, and the finish table doesn't have any references to the other tables.)