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

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".

Related

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.

Inserting into Output table in an insert with from clausule [duplicate]

This question already has answers here:
Using OUTPUT clause to insert value not in INSERTED
(2 answers)
Closed 4 years ago.
I'm trying to duplicate some rows in a table, with a couple of changed values, and I also need to store an old (that will be lost) id to do further processing later. I'm trying to use the Output Clausule to store that information, but SQL Server is throwing the following error:
Msg 4104 <...> The multi-part identifier could not be bound.
This is the table I'm duplicating the data (slightly modified to reduce the number of columns):
Create Table Elements
(
id int Identity(0,1) not null, --PK
name varchar(50) not null,
modelID int not null, --FK
constraint PK_Elements primary key (id)
);
And this is my my query:
declare #outputTable table
(
oldElementID int,
id int,
name varchar(50),
modelID bigint
);
Insert into Elements
(name, modelID)
Output e.id as oldElementID,
Inserted.id,
Inserted.name,
Inserted.modelID into #outputTable
select e.name, #newModelID
from Elements as e
where e.modelID = #oldModelID
Note: #oldModelID and #newModelID are previously declared and set.
I'm not sure if my logic is wrong and I have to take a different approach (but I was sure it was possible to do it this way). Or if I simply have an error that I can't quite put my finger on it.
Any help would be appreciated.
Thanks!
I recreated the problem like this:
CREATE TABLE #a (a INT, b INT)
INSERT INTO #a (a,b) VALUES (42, 43)
INSERT INTO #a (a, b)
OUTPUT a.a, a.b, inserted.a, inserted.b
SELECT a.b, a.a
FROM #a a
The insert operation produces the messages:
Msg 4104, Level 16, State 1, Line 7
The multi-part identifier "a.a" could not be bound.
Msg 4104, Level 16, State 1, Line 7
The multi-part identifier "a.b" could not be bound.
that's because the INSERT command cannot see the alias 'a' that I used in the select command.

Error String or binary data would be truncated

I've been trying to create tables and add data into but I've run into this error
Msg 8152, Level 16, State 14, Line 35
String or binary data would be truncated.
Code:
CREATE TABLE Speakers_photos
(
SpeakerID CHAR(10) NOT NULL,
Image VARBINARY(MAX) NOT NULL,
PRIMARY KEY(SpeakerID),
FOREIGN KEY (SpeakerID) REFERENCES Speakers(SpeakerID)
ON UPDATE CASCADE ON DELETE NO ACTION
)
INSERT INTO Speakers_photos VALUES('S001210001', 0)
Where did it go wrong?
The truncation error will not occur with the code provided. Either the script is incomplete or the error is in an INSERT trigger on the Speakers_photos table. In the case of a trigger, look at line 35 as indicated in the error message.

Type Mismatch Null value SQL

I'm having a problem with Null values in my CREATE TABLE for some reason...It's giving me this error message:
Msg 4864, Level 16, State 1, Line 73
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 4, column 7 (Manager).
Here's my code and the data I'm using:
CREATE TABLE SalesReps
(
EmpNum SMALLINT NOT NULL ,
Name VARCHAR(20) NOT NULL,
Age TINYINT NOT NULL,
RepOffice TINYINT NULL,
Title VARCHAR(20) NULL,
HireDate DATE,
Manager INT NULL,
Quota MONEY NULL,
Sales MONEY DEFAULT 0
)
BULK INSERT SalesReps
FROM 'C:\Users\Steve\Desktop\salesreps.txt'
WITH ( FIELDTERMINATOR = '|',
ROWTERMINATOR = '\n')
Data:
105|Bill Adams|37|13|Sales Rep|02/12/88|104|350000.00|367911.00
109|Mary Jones|31|11|Sales Rep|10/12/89|106|300000.00|392725.00
102|Sue Smith|48|21|Sales Rep|12/10/86|108|350000.00|474050.00
106|Sam Clark|52|11|VP Sales|06/14/88|NULL|275000.00|299912.00
104|Bob Smith|33|12|Sales Mgr|05/19/87|106|200000.00|142594.00
101|Dan Roberts|45|12|Sales Rep|10/20/86|104|300000.00|305673.00
110|Tom Snyder|41|NULL|Sales Rep|01/13/90|101|NULL|75985.00
108|Larry Fitch|62|21|Sales Mgr|10/12/89|106|350000.00|361865.00
103|Paul Cruz|29|12|Sales Rep|03/01/87|104|275000.00|286775.00
107|Nancy Angelli|49|22|Sales Rep|11/14/88|108|300000.00|186042.00
Can anyone please help? I've looked at the other mismatch pages but they aren't helping much. I've been stuck on this for days.
The bulk insert on row 4 includes a value NULL, but I think that SQL Server interprets this as string containing 'NULL'. You can try to change row 4 with that :
106|Sam Clark|52|11|VP Sales|06/14/88||275000.00|299912.00
You will also have the same problem on row 7, your column Quota which expects a MONEY type, but a string containing NULL is provided.

Error while altering table in SQL Server

I'm just trying to add 2 columns to customer table
alter table CUSTOMER ADD ( CUSTOMERNAME VARCHAR(255) NULL
, CUSTOMERDESC VARCHAR(30) NULL )
but I get the following error when I try to run the script,
Msg 102, Level 15, State 1, Line 1 Incorrect syntax near '('.
Can someone tell me what is the mistake that I'm doing. Thanks in anticipation
You don't need the brackets for an alter statement so remove them. ie:
alter table CUSTOMER ADD CUSTOMERNAME VARCHAR(255) NULL,
CUSTOMERDESC VARCHAR(30) NULL
You don't need any ( before the column names - just use:
ALTER TABLE dbo.Customer
ADD CustomerName VARCHAR(255) NULL, CustomerDesc VARCHAR(30) NULL
Does that work?