SQL Error : "Incorrect syntax near" - sql-server-2008

I have a database that is connected to SQL Server 2008, I am getting an error when printing database : Printing aborted! Error 37000 (Microsoft OLE DB Provider for ODBC Drivers) - [Microsoft] [ODBC SQL Server Driver] [SQL Server] [Incorrect syntax near 'TEMP_TAB_SHEET_U17'.
Details script as below :
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [data].[TEMP_TAB_SHEET_U17](
[TAL] [varchar](30) NULL,
[Phase] [smallint] NOT NULL,
[SHT] [varchar](30) NULL,
[PreferedSYS] [varchar](30) NULL,
[sysdesc] [varchar](100) NULL,
[EquipmentNumber] [varchar](30) NULL,
[TaskNumber] [varchar](30) NULL,
[TaskId] [int] NULL,
[OTdescription] [varchar](50) NULL,
[OTremark] [varchar](50) NULL,
[Estimated_Man_Hours] [float] NULL,
[EquipmentId] [int] NULL,
[EquipmentDescription] [varchar](50) NULL,
[TAA_ID] [int] NULL,
[UserGroup] [varchar](30) NULL,
[MODULE] [varchar](30) NULL,
[ModuleDesc] [varchar](50) NULL,
[SHTTitle1] [varchar](50) NULL,
[SHTTitle2] [varchar](50) NULL,
[SHTTitle3] [varchar](50) NULL,
[SHTTitle4] [varchar](50) NULL,
[SHTDescription] [varchar](50) NULL,
[SHTNbSections] [int] NULL,
[NbMaxTasksPerSheet] [int] NULL,
[S1_PH] [varchar](30) NULL,
[S1_M] [varchar](30) NULL,
[S1_PF] [varchar](30) NULL,
[S2_PH] [varchar](30) NULL,
[S2_M] [varchar](30) NULL,
[S2_PF] [varchar](30) NULL,
[S3_PH] [varchar](30) NULL,
[S3_M] [varchar](30) NULL,
[S3_PF] [varchar](30) NULL,
[SHTFooterTitle1] [varchar](50) NULL,
[SHTFooterTitle2] [varchar](50) NULL,
[SHTFooterTitle3] [varchar](50) NULL,
[SHTFooterTitle4] [varchar](50) NULL,
[SHTFooterTitle5] [varchar](50) NULL,
[SHTFooterTitle6] [varchar](50) NULL,
[SHTFooterTitle7] [varchar](50) NULL,
[SHTFooterTitle8] [varchar](50) NULL,
[SHTFooterTitle9] [varchar](50) NULL,
[SHTFooterTitle10] [varchar](50) NULL,
[SHTFooterTitle11] [varchar](50) NULL,
[SHTFooterTitle12] [varchar](50) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [data].[TEMP_TAB_SHEET_U17] ADD DEFAULT ((0)) FOR [Phase]
GO
Query completed running with the error :
Msg 2714, Level 16, State 6, Line 2
There is already an object named 'TEMP_TAB_SHEET_U17' in the database.
Msg 1781, Level 16, State 1, Line 2
Column already has a DEFAULT bound to it.
Msg 1750, Level 16, State 0, Line 2
Could not create constraint. See previous errors.
How to solve this problem.
Thank you in advance.
Awan

Have you tried to set the constrain on creation:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [data].[TEMP_TAB_SHEET_U17](
[TAL] [varchar](30) NULL,
[Phase] [smallint] NOT NULL DEFAULT 0,
...
....
GO
SET ANSI_PADDING OFF
GO

Related

Convert MSSQL to mysql table

Can I know the easiest way to convert the Microsoft sql query to MYSQL.
sample query of Microsoft sql query:
CREATE TABLE [dbo].[T_NEWBIZ](
[newbID] [int] IDENTITY(1,1) NOT NULL,
[planID] [int] NULL,
[applID] [int] NULL,
[taxqID] [int] NULL,
[doptID] [int] NULL,
[stcoID] [int] NULL,
[crsnID] [int] NULL,
[newbEPBR] [bit] NULL,
[newbPolicyNo] [varchar](50) NULL,
[newbEffectiveDate] [varchar](20) NULL,
[newbIssueDate] [varchar](20) NULL,
[newbDistributorOrderNumber] [varchar](30) NULL,
[newbJointAnnuitantNA] [bit] NULL,
[newbOwnerIsAnnuitant] [bit] NULL,
[newbDeliveryEMail] [varchar](100) NULL,
[newbDeliveryConsent] [bit] NULL,
[newbAnnuityOptionSpecialRequest] [varchar](500) NULL,
[newbCSRID] [varchar](20) NULL,
[newbBrokerID] [varchar](10) NULL,
[newbUGMA] [bit] NULL,
[newbUTMA] [bit] NULL,
[newbRecapAvailable] [bit] NULL,
[newbOriginatorCode] [varchar](8) NULL,
[newbFileNetStatus] [char](1) NULL,
[newbJointOwnerNA] [bit] NULL,
[newbNameChange] [bit] NULL,
[newbDateCompleted] [datetime] NULL,
[newbSuitabilityStatus] [varchar](25) NULL,
[newbClientNumber] [varchar](15) NULL,
[newbCheckPayable] [bit] NULL,
[newbMSANumber] [varchar](11) NULL,
[newbPrincipalReviewer] [varchar](10) NULL,
[newbCreationDate] [datetime] NULL,
[newbAnyExistingPolicies] [varchar](1) NULL,
[newbCreatedBy] [varchar](50) NULL,
[newbReplaceExistingPolicy] [varchar](1) NULL,
[newbFormsVerified] [bit] NULL,
[newbTransfer1035] [varchar](1) NULL,
[newbFormsComment] [varchar](500) NULL,
[newbADRequired] [bit] NULL,
[newbAssignedCSRID] [varchar](10) NULL,
[newbBackupWithholding] [varchar](1) NULL,
[newbSuitabilityApprovedDate] [datetime] NULL,
[newbExchangeInternal] [bit] NULL,
[newbAnnuityDate] [varchar](20) NULL,
[newbAnnuityDateOverride] [bit] NULL,
[newbAnnuityOption] [varchar](1) NULL,
[newbSpecialRequest] [varchar](500) NULL,
[newbSpecialRequestNA] [bit] NULL,
[newbFraudAccepted] [bit] NULL,
[newbHireDate] [varchar](20) NULL,
[newbAppless] [bit] NULL,
[newbMailToClient] [bit] NULL,
[newbPGR] [bit] NULL,
[newbDataLock] [varchar](8) NULL,
[amlsCode] [varchar](1) NULL,
[newbFEPlanCodeIndicator] [bit] NULL,
[newbFEPlanCodeDate] [varchar](10) NULL,
[newbBestPlusClientID] [varchar](10) NULL,
[newbDetachedOfficeIndicator] [char](1) NULL,
[newbDetachedOfficeCode] [varchar](5) NULL,
[newbPreviousWFM] [bit] NULL,
[newbARDForm1] [bit] NULL,
[newbARDForm2] [bit] NULL,
[newbARDForm3] [bit] NULL,
[newbARDForm4] [bit] NULL,
[newbARDForm5] [bit] NULL,
[newbPSO] [bit] NULL,
CONSTRAINT [PKC_NewBiz_cnewbID] PRIMARY KEY NONCLUSTERED
(
[newbID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 95) ON [PRIMARY]
) ON [PRIMARY]
You can use mysql workbench to do so, I converted mssql database to MySQL database.
Here is the link to tutorial, it has all the details along with screenshots.

How can I move one schema into another with different fields?

I created an application to replace a legacy one that we had for a while, and I need to move the old database records into the new system, but the schemas are not the same. I'm wondering if there is any way to move the old schema into the new, ignoring the fields that don't exist in the new table, and moving fields that have changed to their updated version.
My old schema is Microsoft SQL Server and has the following fields:
[req_id] [int] IDENTITY(1,1) NOT NULL,
[req_user_id] [nvarchar](50) NOT NULL,
[req_subject] [nvarchar](200) NOT NULL,
[req_details] [nvarchar](4000) NOT NULL,
[req_request_date] [date] NOT NULL,
[req_year] [smallint] NOT NULL,
[req_expect_date] [date] NOT NULL,
[leader] [nvarchar](10) NULL,
[member1] [nvarchar](10) NULL,
[member2] [nvarchar](10) NULL,
[member3] [nvarchar](10) NULL,
[member4] [nvarchar](10) NULL,
[member5] [nvarchar](10) NULL,
[member6] [nvarchar](10) NULL,
[status_code] [nvarchar](10) NULL,
[hours_used] [int] NULL,
[completed_date] [date] NULL,
[category_code] [nvarchar](10) NULL,
[staff_comments] [nvarchar](2000) NULL,
[response_Email] [bit] NOT NULL,
[response_Phone] [bit] NOT NULL,
[response_Fax] [bit] NOT NULL,
[response_online_upload] [bit] NOT NULL,
[response_post_mail] [bit] NOT NULL,
[response_file] [bit] NOT NULL,
[response_pickup] [bit] NOT NULL,
My new schema is MySQL and looks like this:
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`userid` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`subject` varchar(200) COLLATE utf8_unicode_ci NOT NULL,
`details` text COLLATE utf8_unicode_ci NOT NULL,
`eta` date NOT NULL,
`leader` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
`member1` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
`member2` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
`member3` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
`member4` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
`member5` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
`member6` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
`status` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`time_spent` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
`date_completed` date DEFAULT NULL,
`category` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
`comments` text COLLATE utf8_unicode_ci,
`response_method` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
Is there any way to do this and salvage our old records in the new system?
I used the Data Export tool to generate an xlsx file, then used Navicat to import it as a new table to the database, and then used simple inserts statements to move the data to the other table. I was appalled by the response this question got on SO, but I'm glad I found a solution on my own.

SQL INSERT statement conflicted with the CHECK constraint

I am getting this error
InsertRecord(InsertRow): HResult of -2147217873 (80040e2f)
Error Source: Microsoft OLE DB Provider for SQL Server Error
Description : The INSERT statement conflicted with the CHECK constraint CK_ContactName. The conflict occurred in database EPIC.7.5_GR_SM_ETLTRAINING; table dbo.ContactName.
The Statements
CREATE TABLE [dbo].[ContactName](
[UniqContactName] [dbo].[DM_UNIQID] IDENTITY(65536,1) NOT NULL,
[UniqFixedContactName] [dbo].[DM_UNIQID] NOT NULL,
[UniqEntity] [dbo].[DM_UNIQID] NOT NULL,
[LkPrefix] [dbo].[DM_PREFIX] NOT NULL,
[FullName] [dbo].[DM_NAME] NOT NULL,
[FirstName] [varchar](30) NOT NULL,
[MiddleName] [varchar](16) NOT NULL,
[LastName] [varchar](30) NOT NULL,
[LkSuffix] [dbo].[DM_SUFFIX] NOT NULL,
[DescriptionOf] [dbo].[DM_DESC_050] NOT NULL,
[Title] [dbo].[DM_TITLE] NOT NULL,
[Department] [dbo].[DM_DEPARTMENT] NOT NULL,
[UniqContactAddressMain] [dbo].[DM_UNIQID] NOT NULL,
[UniqContactAddressEmployer] [dbo].[DM_UNIQID] NOT NULL,
[UniqContactNumberMain] [dbo].[DM_UNIQID] NOT NULL,
[UniqContactNumberEmailMain] [dbo].[DM_UNIQID] NOT NULL,
[ContactMethodCode] [char](1) NOT NULL,
[InformalHeading] [varchar](50) NOT NULL,
[FormalHeading] [varchar](50) NOT NULL,
[BirthDate] [dbo].[DM_DATE] NULL,
[GenderCode] [char](1) NOT NULL,
[SSN] [char](9) NOT NULL,
[MaritalStatusCode] [char](1) NOT NULL,
[RelationToInsuredCode] [char](2) NOT NULL,
[LkLanguage] [dbo].[DM_LANGUAGE] NOT NULL,
[Comments] [dbo].[DM_COMMENT] NOT NULL,
[BillingDeliveryCode] [char](1) NOT NULL,
[ServicingDeliveryCode] [char](1) NOT NULL,
[MarketingDeliveryCode] [char](1) NOT NULL,
[CategoryCode] [char](1) NOT NULL,
[EmployerName] [dbo].[DM_NAME] NOT NULL,
[LkOccupation] [dbo].[DM_OCCUPATION] NOT NULL,
[HiredDate] [dbo].[DM_DATE] NULL,
[YearsEmployed] [smallint] NULL,
[YearsPriorEmployer] [smallint] NULL,
[FEIN] [char](9) NOT NULL,
[DUNSNumber] [char](9) NOT NULL,
[CdNAICSCode] [char](6) NOT NULL,
[CdSICCode] [char](8) NOT NULL,
[BusinessTypeCode] [char](2) NOT NULL,
[BusinessTypeOtherDesc] [varchar](50) NOT NULL,
[NumberEmployees] [smallint] NULL,
[NumberMembersManagers] [int] NULL,
[BusinessStartedDate] [datetime] NULL,
[NatureOfBusinessCode] [char](2) NOT NULL,
[NatureOfBusinessOtherDesc] [varchar](50) NOT NULL,
[CreditBureauNameCode] [char](5) NOT NULL,
[CreditBureauNameOtherDesc] [varchar](50) NOT NULL,
[CreditBureauIDNumber] [varchar](30) NOT NULL,
[DriverLicenseNumber] [varchar](25) NOT NULL,
[LicensedState] [char](4) NOT NULL,
[LicensedDate] [datetime] NULL,
[LicensedMADate] [dbo].[DM_DATE] NULL,
[DriverTypeCode] [char](1) NOT NULL,
[GoodStudentCode] [char](1) NOT NULL,
[DriverTrainingCode] [char](1) NOT NULL,
[AccidentPreventionCourseDate] [dbo].[DM_DATE] NULL,
[CommercialExperienceBeganDate] [dbo].[DM_DATE] NULL,
[MatchClientNameOf] [smallint] NOT NULL,
[InsertedByCode] [dbo].[DM_INSERTUPDATEBYCODE] NOT NULL,
[InsertedDate] [dbo].[DM_DATETIME] NOT NULL,
[UpdatedByCode] [dbo].[DM_INSERTUPDATEBYCODE] NOT NULL,
[UpdatedDate] [dbo].[DM_DATETIME] NULL,
[Flags] [dbo].[DM_FLAGS] NOT NULL,
[ts] [datetime] NOT NULL,
[SIN] [char](9) NOT NULL,
[BusinessNumber] [varchar](30) NOT NULL,
[BusinessIDNumber] [varchar](30) NOT NULL,
[IBCCode] [char](6) NOT NULL,
CONSTRAINT [PK_ContactName] PRIMARY KEY NONCLUSTERED ALTER TABLE [dbo].[ContactName]
WITH CHECK ADD CONSTRAINT [CK_ContactName]
CHECK ((
[UniqContactName]=(-1)
AND [UniqEntity]=(-1)
OR [UniqContactName]>(-1)
AND [UniqEntity]>(-1)
))
GO
I have checked contactName db and all the rows from uniqueContactName all have a value above 65000, then for UniqEntity they all are above 65000 as well.
Does anyone have an idea why this would fail?

SSIS get all data from A table that are not in B table and inserted it in table B

i have two table and i need to create SSIS package "data flow task"
table A and table B, i need to create SSIS package that always insert the data that are not in table B from table A into table B (based on badgeno in A and badgeno_ID in B ). any thought and helps plz?
CREATE TABLE [dbo].[A](
[firstname] [nvarchar](100) NULL,
[mobile] [nvarchar](50) NULL,
[note] [nvarchar](500) NULL,
[city] [nvarchar](255) NULL,
[badgeno] [nvarchar](50) NULL,
[lastname] [nvarchar](100) NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[B](
[ID] [int] IDENTITY(1,1) NOT NULL,
[FirstName] [nvarchar](100) NULL,
[LastName] [nvarchar](100) NULL,
[DateOfBirth] [datetime] NULL,
[FirmID] [int] NULL,
[Phone] [varchar](50) NULL,
[Mobile] [varchar](50) NULL,
[EMail] [nvarchar](100) NULL,
[EmployeeNr] [nvarchar](10) NULL,
[Note] [nvarchar](500) NULL,
[PhonePrivate] [varchar](50) NULL,
[StreetAddress] [nvarchar](255) NULL,
[Zip] [nvarchar](20) NULL,
[City] [nvarchar](255) NULL,
[Data] [nvarchar](500) NULL,
[UserName] [nvarchar](50) NULL,
[Password] [nvarchar](50) NULL,
[ADGUID] [nvarchar](50) NULL,
[Language] [nvarchar](50) NULL,
[Theme] [nvarchar](30) NULL,
[DefaultZone] [int] NULL,
[Bed] [nvarchar](50) NULL,
[badgeNO_ID] [nvarchar](50) NULL,
[RegisteredBy] [nvarchar](50) NOT NULL,
[Registered] [datetime] NOT NULL,
[LastUpdatedBy] [nvarchar](50) NOT NULL,
[LastUpdated] [datetime] NOT NULL,
[DefaultView] [nvarchar](50) NULL,
[ExpandTagList] [bit] NULL,
[PrintEmergencyReport] [bit] NULL,
[EmergencyReportPath] [nvarchar](255) NULL)
Drag and drop an Execute Sql Task and put the following t-sql query in there. Jobs a gooden.
INSERT INTO [dbo].[B]
([firstname] , [mobile], [note], [City], [badgeNO_ID] , [LastName])
SELECT [A].[firstname]
, [A].[mobile]
, [A].[note]
, [A].[City]
, [A].[badgeno]
, [A].[LastName]
FROM [dbo].[A]
WHERE NOT EXISTS (SELECT 1
FROM [dbo].[B]
WHERE [dbo].[A].[badgeno] = [dbo].[B].[badgeNO_ID])
Use an SSIS Lookup Transformation with "No Match" Output
http://msdn.microsoft.com/en-us/library/ms141821.aspx
https://www.simple-talk.com/sql/ssis/implementing-lookup-logic-in-sql-server-integration-services/

Subquery returned more than 1 value in insert statement

I need solution to fix this error Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
I don't have subquery in my insert statement but still I am getting this error.
Code:
INSERT INTO enquiries_dtls(confdate,confname,region,enquiry,attendedby,dctname,
hospital,contactno,email,addr)
VALUES('10/04/2013',
'APAO',
'Delhi',
'test',
'test',
'test',
'test',
'123214',
'abc#abc.com',
'test')
DDL:
CREATE TABLE [dbo].[enquiries_dtls]
(
[sno] [int] IDENTITY(1,1) NOT NULL,
[Timestamp] [timestamp] NULL,
[confdate] [date] NULL,
[confname] [varchar](max) NULL,
[region] [varchar](50) NULL,
[enquiry] [varchar](max) NULL,
[attendedby] [varchar](50) NULL,
[dctname] [varchar](50) NULL,
[hospital] [varchar](50) NULL,
[contactno] [int] NULL,
[email] [varchar](50) NULL,
[addr] [varchar](max) NULL,
[stsupdate] [varchar](max) NULL,
[dtupdate] [date] NULL,
[closingdate] [date] NULL,
[clsedby] [varchar](50) NULL,
[revenue] [varchar](50) NULL,
[apprstatus] [varchar](50) NULL,
[clsed] [varchar](50) NULL,
[enqId] [varchar](50) NULL
);