Unclosed quotation mark after the character - sql

I try to update my table by creating a temporary table, when I run the query in my local DB i get no issue, but when I run it in production DB I get this issue:
Msg 105, Level 15, State 1, Line 1012
Unclosed quotation mark after the character string 'PostCode
'.
Msg 102, Level 15, State 1, Line 1012
Incorrect syntax near 'PostCode
'.
Here is the line of code that throws the error:
INSERT [dbo].[#TempPostalCode] ([PostCode], [PostIn], [Bus], [CustomerPLId], [Box-In_Service])
VALUES (13189, 1, 1, 0, 0)
Update:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[#TempPostalCode](
[PostCode] [float] NULL,
[PostIn] [float] NULL,
[Bus] [float] NULL,
[CustomerPLId] [float] NULL,
[Box-In_Service] [float] NULL
) ON [PRIMARY]
GO

Related

Merge stored procedure

CREATE TABLE [dbo].[SupplierTableType](
[Supplier_ID] [nvarchar](10) NULL,
[Supplier_Name] [nvarchar](50) NULL,
[Address1] [nvarchar](30) NULL,
[Address2] [nvarchar](30) NULL,
[Address3] [nvarchar](30) NULL,
)
GO
--------------------Procedure----------------------
CREATE PROCEDURE [dbo].[Update_SupplierTable]
#TblSupplierTable SupplierTableType READONLY
AS
BEGIN
SET NOCOUNT ON;
MERGE INTO SupplierTable c1
USING #TblSupplierTable c2
ON c1.Supplier_ID=#TblSupplierTable.Supplier_ID
WHEN MATCHED THEN
UPDATE SET
c1.[Supplier_Name]= c2.[Supplier_Name]
, c1.[Address1]=c2.[Address1]
, c1.[Address2]=c2.[Address2]
, c1.[Address3]=c2.[Address3]
WHEN NOT MATCHED THEN
INSERT VALUES( #TblSupplierTable.[Supplier_Name],
c2.[Address1],
c2.[Address2],
c2.[Address3]
);
END
Msg 2715, Level 16, State 3, Procedure Update_SupplierTable, Line 3 [Batch Start Line 56]
Column, parameter, or variable #1: Cannot find data type SupplierTableType.
Parameter or variable '#TblSupplierTable' has an invalid data type.
Msg 1087, Level 16, State 1, Procedure Update_SupplierTable, Line 8 [Batch Start Line 56]
Must declare the table variable "#TblSupplierTable".
Instead of creating a table, you have to create a data type for SupplierTableType.
I.e. instead of
CREATE TABLE SupplierTableType...
you have to use
CREATE TYPE SupplierTableType AS TABLE ...
See the docs for further info.

Msg 207, Level 16, State 1 "Invalid column name 'Name'"

I'm trying to drop and recreate a table with a new schema in SQL Server, and then insert some test data into it to validate that it's working correctly. I have the following DDL:
CREATE TABLE [dbo].[Product](
[ID] int IDENTITY(1,1) PRIMARY KEY,
[Name] VARCHAR(100) NOT NULL,
[Description] VARCHAR(200) NULL,
[Modified] DATETIME NOT NULL DEFAULT(GETUTCDATE()),
[ModifiedBy] VARCHAR(32) NOT NULL
);
and the following insert statement:
INSERT INTO [dbo].[Product]([Name], [Description], [Modified], [ModifiedBy])
VALUES('Test', 'Description', GETUTCDATE(), 'Me');
Whenever I attempt to insert the aforementioned row, I get an error:
Msg 207, Level 16, State 1, Line 38
Invalid column name 'Name'.
Msg 207, Level 16, State 1, Line 38
Invalid column name 'Description'.
I know the table is there since I can SELECT * FROM Product and get an empty result set with the correct columns and no errors... I just can't insert into it for some reason. Any help would be greatly appreciated!
As per Martin's answer, I did some more research and he is correct. The schema manipulation needs to be done in separate batches (I simply added a GO after dropping my old table and after creating my new one with a different schema). A more detailed answer can be found here: "Invalid column name" error when calling insert after table created
Maybe try refreshing the cache. Easy way is to press Ctrl + shift + R
Or
Edit > IntelliSense > Refresh Local Cache
If doesnt work, then use qualified names [dbo].table or username.tablename etc
More here: http://msdn.microsoft.com/en-us/library/ms174205.aspx
For me, your query looks good, and i test it successfully.
Try adding schema name and put all column name inside brackets:
CREATE TABLE [dbo].[Product](
[ID] int IDENTITY(1,1) PRIMARY KEY,
[Name] VARCHAR(100) NOT NULL,
[Description] VARCHAR(200) NULL,
[Modified] DATETIME NOT NULL DEFAULT(GETUTCDATE()),
[ModifiedBy] VARCHAR(32) NOT NULL
);
INSERT INTO [dbo].[Product]([Name], [Description], [Modified], [ModifiedBy])
VALUES('Test', 'Description', GETUTCDATE(), 'Me');
Don't do anything just try to insert (' ')single quotes instead of (") quotes.. or if you insert data with ('')single quotes try for (") double quotes it's really helping you.

Dynamic Variable Name for backup table

I am trying to write a stored procedure to backup a table, but I keep getting:
Msg 402, Level 16, State 1, Line 9
The data types varchar and datetime2 are incompatible in the add operator.
Msg 402, Level 16, State 1, Line 15
The data types varchar and datetime2 are incompatible in the add operator.
How can I fix this?
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
DECLARE #CreateDynamicSQL nvarchar(1000);
DECLARE #CopyDynamicSQL nvarchar(1000);
SET #CreateDynamicSQL='CREATE TABLE [dbo].[paul_AccountContact_Backup_'+#SYSDATETIME+'](
[AccountID] [int] NOT NULL,
[ContactID] [int] NOT NULL
) ON [PRIMARY]
GO'
SET #CopyDynamicSQL='select * into [dbo].[paul_AccountContact_Backup_'+#SYSDATETIME+'] from paul_AccountContacts'
EXEC(#CreateDynamicSQL);
EXEC(#CopyDynamicSQL);
You have to convert your date variable to a string so you can concatenate it:
SET #CreateDynamicSQL='CREATE TABLE [dbo].[paul_AccountContact_Backup_'
+ convert(varchar(20), #SYSDATETIME, <Format>) + '](
Documentation for the format parameter:
https://learn.microsoft.com/en-us/sql/t-sql/functions/cast-and-convert-transact-sql?view=sql-server-2017

Error when inserting into a simple two column table with SQL Server 2012?

I am running the following:
insert into [authentication].[dbo].[AspNetUserRoles] ("RoleId","UserId")
values ("0918fb0f-79c0-4298-b184-9a754dc5c30e", "527ddbd5-14d3-4fb9-a7ae-374e66f635d4")
Here's my table:
CREATE TABLE [dbo].[AspNetUserRoles] (
[RoleId] NVARCHAR (128) NOT NULL,
[UserId] NVARCHAR (128) NOT NULL
);
However it's giving me an error saying:
Msg 207, Level 16, State 1, Line 2
Invalid column name '0918fb0f-79c0-4298-b184-9a754dc5c30e'.
Is there something wrong with the way I have coded the insert ?
Put the values in '' not "":
insert into [authentication].[dbo].[AspNetUserRoles] ("RoleId","UserId")
values ('0918fb0f-79c0-4298-b184-9a754dc5c30e',
'527ddbd5-14d3-4fb9-a7ae-374e66f635d4');
The problem is that the values with "" around them are treated as identifiers not literals. Like in "RoleId","UserId".

SQL Server 2005 - Bulk Insert failing

I have a txt file that contains 1600 rows and 82 columns of comma delineated data that I am trying to import into a table. I get the following error on every row on the very last field:
Msg 4864, Level 16, State 1, Line 1
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 1, column 81 (DB252D20C8).
The import statement is
BULK
INSERT [ENERGY].[dbo].[READINGS1]
from 'c:\readings2.txt'
with
(
DATAFILETYPE='widechar',
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
GO
The table structure is as follows, the top and bottom of the script:
USE [ENERGY]
GO
/****** Object: Table [dbo].[READINGS1] Script Date: 05/13/2013 20:00:30 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[READINGS1](
[DateAndTime] [datetime] NOT NULL,
[DB240D4C7] [float] NULL,
[DB240D8C7] [float] NULL,
[DB240D12C7] [float] NULL,
[DB240D16C7] [float] NULL,
[DB252D12C8] [float] NULL,
[DB252D16C8] [float] NULL,
[DB252D20C8] [float] NULL,
CONSTRAINT [READINGS1DataTimeStamp] PRIMARY KEY CLUSTERED
(
[DateAndTime] ASC
)WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
The text file is as follows:
2013-02-19 00:00:00.000,6,945,1886,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,22,2040,6299,0,0,6,567,1248,0,0,251,8859,8655,0,0,10,316,1786,0,0,7,180,1206,0,0,1,16,56,0,0,368,18953,36949,0,0,NULL,NULL
2013-02-19 01:00:00.000,6,147,1886,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,22,1516,6299,0,0,3,115,1248,0,0,250,5077,8655,0,0,9,219,1786,0,0,5,147,1206,0,0,1,15,56,0,0,362,8907,36949,0,0,NULL,NULL
Alright so what you need to do is alter your statement so that after the end of the file you use KEEPNULLS. This informs SQL server that you wish to keep your null values. Currently it's trying to convert NULL as a string into your FLOAT COLUMN. Alter your statment to look like this.
BULK
INSERT [ENERGY].[dbo].[READINGS1]
from 'c:\readings2.txt'
with
(
DATAFILETYPE='widechar',
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n',
KEEPNULLS
)
GO
There is an article on BOL about this. .
Otherwise you can always build a Integration Services package to handle this. That is an easy fast way to import information from flat file sources.
It turns out that there were too many fields in the input text file for the table.