I am using the code below:
Insert into JV(PurchaseID)
select PurchaseID from PurchaseReturnDetail
But when I execute the query it gives an error:
Cannot insert the value NULL into column 'column1', table 'database.table1'; column does not allow nulls.
Then my INSERT fails. What could be the issue?
The error says the table has a non-null constraint for the column. To change the column to allow nulls, run this query (for Oracle DB):
alter table TABLE_NAME modify (COLUMN_NAME null);
If first columns is identity column, make sure that auto increment is enabled for that column .
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[JV](
[JVID] [numeric](18, 0) IDENTITY(1,1) NOT NULL,
[JVInvoiceNo] [nvarchar](50) NULL,
[JVDate] [datetime] NULL,
[StoreID] [numeric](18, 0) NULL,
[BusinessID] [numeric](18, 0) NULL,
[UserID] [numeric](18, 0) NULL,
[Currency] [nvarchar](50) NULL,
[Rate] [decimal](18, 10) NULL,
[DueDate] [datetime] NULL,
[Reference] [nvarchar](50) NULL,
[RefID] [numeric](18, 0) NULL,
[Narration] [nvarchar](max) NULL,
[InvoiceRef] [nvarchar](50) NULL,
[Status] [nvarchar](50) NULL,
[Type] [nvarchar](50) NULL,
[PurchaseID] [numeric](18, 0) NULL,
CONSTRAINT [PK_JV] PRIMARY KEY CLUSTERED
(
[JVID] 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
Related
I need to have Select up to 3 company Types in SQL and use check constrain.
How to do this, or I also accept other suggestions., For the following table:
CREATE TABLE Dbo.[CompanyType](
[TypeID] [bigint] IDENTITY(1,1) NOT NULL,
[Manufacturer] [bit] NULL,
[Trading] [bit] NULL,
[BuyingOffice] [bit] NULL,
[Agent] [bit] NULL,
[Wholesaler] [bit] NULL,
[Commission] [bit] NULL,
[Association] [bit] NOT NULL,
[BusinessService] [bit] NOT NULL,
[Other] [bit] NOT NULL,
[Photo] [image] NULL,
[CreateDate] datetime
CONSTRAINT [PK_CompanyType] PRIMARY KEY CLUSTERED
(
[TypeID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
ALTER TABLE dbo.[CompanyType] ADD CONSTRAINT [DF_Company_PersianTax] DEFAULT ((9)) FOR [Taxpercent]
GO
ALTER TABLE dbo.[CompanyType] ADD CONSTRAINT [DF_Company_CreateDate] DEFAULT (getdate()) FOR [CreateDate]
GO
using a check constrain.
How to use check constrain for this model??
I tried by :
In the meantime I use SP to insert. Isn't it better to check for three or more true data there? At the moment of data entry as a parameter check
--parameter validation
if(#Manufacturer+#Trading+#BuyingOffice+#Agent+#Wholesaler+#Commission+#Association+#BusinessService+#Other)<= 3
Begin
Return
End
else
insert statement (...)
Using trigger like following query! check inserted, sum of column <= 3 rolback
CREATE TRIGGER usp_checksum3
ON dbo.companytype
AFTER insert
AS
BEGIN
SET NOCOUNT ON;
if (Select [Manufacturer]+[Trading]+[BuyingOffice]+[Agent]+[Wholesaler]+[Commission]+[Association]+[BusinessService]+[Other]
From inserted) <= 3
Return
END
Thanks for your tips
Consider using the following check constraint:
CREATE TABLE Dbo.[CompanyType](
[TypeID] [bigint] IDENTITY(1,1) NOT NULL,
[Manufacturer] [bit] NULL,
[Trading] [bit] NULL,
[BuyingOffice] [bit] NULL,
[Agent] [bit] NULL,
[Wholesaler] [bit] NULL,
[Commission] [bit] NULL,
[Association] [bit] NOT NULL,
[BusinessService] [bit] NOT NULL,
[Other] [bit] NOT NULL,
[Photo] [image] NULL,
[CreateDate] datetime
CONSTRAINT [PK_CompanyType] PRIMARY KEY CLUSTERED ([TypeID] ASC)
WITH (
PAD_INDEX = OFF,
STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON,
OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY],
CONSTRAINT [PK_Company_Types] CHECK (
CAST([Manufacturer] AS INT)
+ CAST([Trading] AS INT)
+ CAST([BuyingOffice] AS INT)
+ CAST([Agent] AS INT)
+ CAST([Wholesaler] AS INT)
+ CAST([Commission] AS INT)
+ CAST([Association] AS INT)
+ CAST([BusinessService] AS INT)
+ CAST([Other] AS INT)
<= 3)
You don't need to implement additional logic in your stored procedure. The check constraint guarantees integrity, and apply equally to inserts performed from the SP or outside.
If you want to modify the existing table:
ALTER TABLE Dbo.[CompanyType]
ADD CONSTRAINT [Chk_CompanyType] CHECK (
CAST([Manufacturer] AS INT)
+ CAST([Trading] AS INT)
+ CAST([BuyingOffice] AS INT)
+ CAST([Agent] AS INT)
+ CAST([Wholesaler] AS INT)
+ CAST([Commission] AS INT)
+ CAST([Association] AS INT)
+ CAST([BusinessService] AS INT)
+ CAST([Other] AS INT)
<= 3)
INSERT INTO dbo.SaleNew(ENQ_AID,DOCKET_NO,SALE_TYPE,VEHICLE_MODEL,SALE_DATE,BOOKING_DATE,DELIVERY_DATE,
DEALER_NAME,ENQ_GEN_BY,EXEC_NAME,USER_CR,DATE_CR) SELECT '6','2','0','TEST','2016-05-01','2016-05-10','2016-05-15',
'ABC','S I','V B','1',SWITCHOFFSET(SYSDATETIMEOFFSET(), '+05:30')
What could be wrong with this query?
Also tried:
INSERT INTO dbo.SaleNew(ENQ_AID,DOCKET_NO,SALE_TYPE,VEHICLE_MODEL,SALE_DATE,BOOKING_DATE,DELIVERY_DATE,
DEALER_NAME,ENQ_GEN_BY,EXEC_NAME,USER_CR,DATE_CR) SELECT '6','2','0','TEST',CONVERT(DATE,'01/05/2016',103),CONVERT(DATE,'10/05/2016',103),
CONVERT(DATE,'15/05/2016',103),'ABC','S I','V B','1',SWITCHOFFSET(SYSDATETIMEOFFSET(), '+05:30')
This is the create table query:
CREATE TABLE [dbo].[SaleNew](
[SALE_ID] [int] IDENTITY(1,1) NOT NULL,
[ENQ_AID] [bigint] NOT NULL,
[DOCKET_NO] [varchar](50) NOT NULL,
[VEHICLE_MODEL] [varchar](100) NOT NULL,
[SALE_DATE] [date] NULL,
[BOOKING_DATE] [date] NULL,
[DELIVERY_DATE] [date] NULL,
[DEALER_NAME] [date] NULL,
[ENQ_GEN_BY] [varchar](100) NULL,
[EXEC_NAME] [varchar](100) NULL,
[USER_CR] [int] NULL,
[DATE_CR] [date] NULL,
[USER_UP] [int] NULL,
[DATE_UP] [date] NULL,
[SALE_TYPE] [int] NOT NULL,CONSTRAINT [PK_SaleNew] PRIMARY KEY CLUSTERED (
[SALE_ID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON ) ON [PRIMARY]
What could be the reason?
DEALER_NAME type is defined as DATE but you want to INSERT a VARCHAR type value ABC in it.
Execute following query in order to edit the mentioned column type, then Execute your INSERT query.
ALTER TABLE [dbo].[SaleNew]
ALTER COLUMN [DEALER_NAME] VARCHAR(50) NULL
I know this is a common topic but i stil feel my scenario requires some custom advice. I was selecting from a table the other day and it took me an AGE to select on a datetime column that has not been indexed. I want to index this, only problem ebing is that the "production" 2005 box i am unfamiliar with and how it will handle the index creation. With that in mind i am wondering what the safest way for me to create an index on the table is. All the details i think people will need are below (i hope) :
Index to be created on field 5 (possibly field4 also)
Table 1 definition:
CREATE TABLE [dbo].[TABLE 1](
[ID] [uniqueidentifier] NOT NULL CONSTRAINT [DF_BLA] DEFAULT (newid()),
[field1] [nvarchar](7) NULL,
[field2] [nvarchar](10) NULL,
[field3] [nvarchar](2) NULL,
[field4] [datetime] NULL,
[field5] [datetime] NULL,
[field6] [smallint] NULL,
[field7] [nvarchar](1) NULL,
[field8] [nvarchar](7) NULL,
[field9] [nvarchar](60) NULL,
[field10] [smallint] NULL,
[field11] [nvarchar](15) NULL,
[field12] [datetime] NULL,
CONSTRAINT [PK_ID] 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]
Total count of rows in Table 1 : 2926836
I am executing the query on SQL server on hosting and it is taking 1 minute and 35 seconds. And the no, of rows of retrieval are 18000. Still it is taking too much time. Query is
select ID,
FirstName,
LastName,
Branch,
EnquiryID,
Course,
College,
Mobile,
ExamID,
EntranceID,
Entrance,
Venue,
RegNo,
VenueID,
Exam,
Gender,
row_number() over (partition by EnquiryID order by ID asc) as AttemptNO
from AGAM_View_AOPList
order by EnquiryID
TABLE SCHEMAS
CREATE TABLE [dbo].[AGAM_AceOFPace](
[ID] [int] IDENTITY(1,1) NOT NULL,
[EnquiryID] [int] NULL,
[FirstName] [nvarchar](100) NULL,
[MiddleName] [nvarchar](100) NULL,
[LastName] [nvarchar](100) NULL,
[BranchID] [int] NULL,
[Branch] [nvarchar](100) NULL,
[CourseID] [int] NULL,
[ExamID] [int] NULL,
[Exam] [nvarchar](200) NULL,
[EntranceID] [int] NULL,
[Entrance] [nvarchar](200) NULL,
[RegNo] [nvarchar](200) NULL,
[EntranceCode] [nvarchar](100) NULL,
[ExamDate] [nvarchar](50) NULL,
[UserID] [nvarchar](100) NULL,
[EntranceFees] [numeric](18, 2) NULL,
[VenueID] [int] NULL,
[Venue] [nvarchar](max) NULL,
[ChequeNumber] [nvarchar](50) NULL,
[Bank] [nvarchar](100) NULL,
[CreatedDate] [datetime] NULL,
CONSTRAINT [PK_AGAM_AceOFPace] 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].[AGAM_AceOFPace] WITH CHECK ADD CONSTRAINT [FK_AGAM_AceOFPace_AGAM_Inquiry] FOREIGN KEY([EnquiryID])
REFERENCES [dbo].[AGAM_Inquiry] ([ID])
GO
ALTER TABLE [dbo].[AGAM_AceOFPace] CHECK CONSTRAINT [FK_AGAM_AceOFPace_AGAM_Inquiry]
GO
SECOND TABLE
CREATE TABLE [dbo].[AGAM_Inquiry](
[ID] [int] IDENTITY(1,1) NOT NULL,
[RegNo] [nvarchar](200) NULL,
[BranchID] [int] NULL,
[Category] [nvarchar](100) NULL,
[CourseID] [int] NULL,
[EntranceFees] [numeric](18, 2) NULL,
[EntranceID] [int] NULL,
[UserID] [nvarchar](50) NULL,
[Status] [nvarchar](50) NULL,
[ReminderDate] [datetime] NULL,
[Reminder] [nvarchar](150) NULL,
[Mobile] [nvarchar](50) NULL,
[Email] [nvarchar](50) NULL,
[FirstName] [nvarchar](50) NULL,
[MiddleName] [nvarchar](50) NULL,
[LastName] [nvarchar](50) NULL,
[Landline] [nvarchar](50) NULL,
[Address] [nvarchar](100) NULL,
[DOB] [datetime] NULL,
[Gender] [nvarchar](50) NULL,
[PfBatchTime] [nvarchar](50) NULL,
[SourceOfInquiry] [nvarchar](50) NULL,
[ExStudentID] [int] NULL,
[InquiryDate] [datetime] NULL,
[ReceiptNumber] [nvarchar](50) NULL,
[RawID] [int] NULL,
[Deleted] [int] NULL,
[CreatedBy] [nvarchar](50) NULL,
[CreatedDate] [datetime] NULL,
[LastModifiedBy] [nvarchar](50) NULL,
[LastModifiedDate] [datetime] NULL,
[College] [nvarchar](150) NULL,
[Qualification] [nvarchar](150) NULL,
[RptNo] [nvarchar](100) NULL,
CONSTRAINT [PK_AGAM_Inquiry] 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].[AGAM_Inquiry] WITH CHECK ADD CONSTRAINT [FK_AGAM_Inquiry_AGAM_Branch] FOREIGN KEY([BranchID])
REFERENCES [dbo].[AGAM_Branch] ([ID])
GO
ALTER TABLE [dbo].[AGAM_Inquiry] CHECK CONSTRAINT [FK_AGAM_Inquiry_AGAM_Branch]
GO
ALTER TABLE [dbo].[AGAM_Inquiry] WITH CHECK ADD CONSTRAINT [FK_AGAM_Inquiry_AGAM_Course] FOREIGN KEY([CourseID])
REFERENCES [dbo].[AGAM_Course] ([ID])
GO
ALTER TABLE [dbo].[AGAM_Inquiry] CHECK CONSTRAINT [FK_AGAM_Inquiry_AGAM_Course]
GO
ALTER TABLE [dbo].[AGAM_Inquiry] WITH CHECK ADD CONSTRAINT [FK_AGAM_Inquiry_AGAM_Users] FOREIGN KEY([UserID])
REFERENCES [dbo].[AGAM_Users] ([UserID])
GO
ALTER TABLE [dbo].[AGAM_Inquiry] CHECK CONSTRAINT [FK_AGAM_Inquiry_AGAM_Users]
GO
Can you try with changing the view to this?
SELECT TOP (100) PERCENT
AP.ID,
AP.FirstName,
AP.LastName,
AP.Branch,
AP.EnquiryID,
AC.Name,
AI.College,
AI.Mobile,
AP.ExamID,
AP.EntranceID,
AP.RegNo,
AP.VenueID,
AP.Exam,
AI.Gender,
AP.BranchID,
AP.CourseID,
AP.CreatedDate,
AI.Status,
AP.Entrance,
AP.Venue
FROM dbo.AGAM_AceOFPace AS AP
INNER JOIN dbo.AGAM_Inquiry AS AI ON AI.ID = AP.EnquiryID
INNER JOIN dbo.AGAM_Course as AC on AC.ID = AP.CourseId
ORDER BY AP.EnquiryID
Do you have an index on EnquiryId and CourseID?
Seeing as you are joining, ordering and partitioning by it you really should.
CREATE INDEX IDX_AGAM_AceOFPace_EnquiryID
ON AGAM_AceOFPace (EnquiryID)
CREATE INDEX IDX_AGAM_AceOFPace_CourseID
ON AGAM_AceOFPace (CourseID)
I have a table called Bowzer which has a column called Type.
When I try to run this query, SSMS does not recognize the column Type.
Here is my query
update Bowzer
set inserted_by = ISNULL(inserted_by, 'Auto')
where (bowzer_id = 8 and inserted_by is null and type='CRUDE')
Here it does not recognize the column Type and gives an error
Invalid Column Name Type
Is this column name Type not allowed?
Also, I want to check if the Inserted_By column is null then replace it with a value Auto
How do i do that?
EDIT - 1 DDL To the Table Bowzer
CREATE TABLE [dbo].[Bowzer](
[bowzer_id] [numeric](18, 0) IDENTITY(1,1) NOT NULL,
[bowzer_no] [nvarchar](20) NULL,
[quantity] [numeric](18, 0) NULL,
[dip1] [numeric](18, 0) NULL,
[Cmpt_Capacity1] [numeric](5, 0) NULL,
[dip2] [numeric](18, 0) NULL,
[Cmpt_Capacity2] [numeric](5, 0) NULL,
[dip3] [numeric](18, 0) NULL,
[Cmpt_Capacity3] [numeric](5, 0) NULL,
[dip4] [numeric](18, 0) NULL,
[Cmpt_Capacity4] [numeric](5, 0) NULL,
[dip5] [numeric](18, 0) NULL,
[Cmpt_Capacity5] [numeric](5, 0) NULL,
[expiry_date] [datetime] NOT NULL,
[status] [bit] NOT NULL,
[inserted_on] [datetime] NULL,
[inserted_by] [nvarchar](20) NULL,
[Type] [nvarchar](50) NULL,
CONSTRAINT [PK_Bowzer] PRIMARY KEY CLUSTERED
(
[bowzer_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
try this
update Bowzer
set inserted_by = ISNULL(inserted_by, 'Auto')
where (bowzer_id = 8 and inserted_by is null and [type]='CRUDE')
for check if null, use Case
Update Bowzer set inserted_by = Case When inserted_by IS NULL THEN 'Auto' Else inserted_by End
where (bowzer_id = 8 and inserted_by is null and [type]='CRUDE')