How to fix this message error that i cant create table using script? - sql

I just want to make a new table in database, when I create table just got some messages like
Msg 102, Level 15, State 1, Line 5 Incorrect syntax near 'tbl_invoice'".
So my question is where do I start to make a new table?
use master
use db_penjualan
create table tbl_invoice (
no_invoice varchar primary key (10),
jenis_pembayaran varchar(25),
user_generate varchar (10),
tgl_terbit date not null )
;
Messages
Msg 102, Level 15, State 1, Line 5 Incorrect syntax near
'tbl_invoice'.

The problem lies with primary key declaration syntax. Write it as:
use db_penjualan
Go
create table tbl_invoice (
no_invoice varchar(10),
jenis_pembayaran varchar(25),
user_generate varchar (10),
tgl_terbit date not null ,
primary key(no_invoice)--define primary key col here
)
;

Related

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.

How do I fix my trigger query so that the multi-part identifiers can be bound?

I'm working on a trigger which will allow me to store all the previous data inside specified columns on my Customer table into a specified audit-table once those columns are updated. The fields I'm taking from the customer table and storing are specific, as there are multiple fields on the customers table and I am only trying to store the (CustomerID, CustomerAddress and CustomerPostcode)
Here is the fields in the Customer table:
[CustomerID]
[CustomerName]
[CustomerAddress]
[CustomerPostcode]
[CustomerTelephone]
[CardNumber]
[CountyID]
I am only trying to take [CustomerID], [CustomerAddress] and [CustomerPostcode]
In order to store these specific fields I set up an audit table to the best of my ability which will store those fields, but also display auto-generated fields based on the trigger.
Here is the Audit-Table I set up:
CREATE TABLE Customer_Audit
(
Cust_UpdateID int IDENTITY (1,1),
Cust_User char (8),
Cust_Update_Date date,
CustomerID int,
CustomerAddresss nvarchar (255),
CustomerPostCode nvarchar (255),
CONSTRAINT [pk_Cust_UpdateID] PRIMARY KEY (Cust_UpdateID)
)
Here is the trigger query I've set up:
CREATE TRIGGER Customer_Update_Trigger ON tblCustomer
AFTER UPDATE
AS
BEGIN
INSERT INTO Customer_Audit (Cust_User, Cust_Update_Date, Cust_ID, CustomerAddresss, CustomerPostCode)
SELECT
CURRENT_USER,
GETDATE(),
d.CustomerID,
d.CustomerAddress,
d.CustomerPostcode
FROM deleted
END
In order to store the data I'm trying to take the fields from the deleted table but every time I do it I keep getting the following error messages:
Msg 4104, Level 16, State 1, Procedure Customer_Update_Trigger, Line 11
The multi-part identifier "d.CustomerID" could not be bound.
Msg 4104, Level 16, State 1, Procedure Customer_Update_Trigger, Line 12
The multi-part identifier "d.CustomerAddress" could not be bound.
Msg 4104, Level 16, State 1, Procedure Customer_Update_Trigger, Line 13
The multi-part identifier "d.CustomerPostcode" could not be bound
I don't know what it is I'm doing wrong, I know the field names in the deleted table match the field names in the customer table, but it still refused to process.
you are missing an alias for deleted, try adding FROM deleted as d
CREATE TRIGGER Customer_Update_Trigger ON tblCustomer
AFTER UPDATE AS
BEGIN;
INSERT INTO Customer_Audit (
Cust_User
, Cust_Update_Date
, Cust_ID
, CustomerAddresss
, CustomerPostCode
)
SELECT
CURRENT_USER
, GETDATE()
, d.CustomerID
, d.CustomerAddress
, d.CustomerPostcode
FROM deleted as d;
END;

Need assistance to create a simple SQL database. Error is as follows: Msg 102, Level 15, State 1, Line 2 Incorrect syntax near ','

I just need a little help with an error I am receiving when trying to create a table in SQL Server 2008 Management Studio. The table I am trying to construct is: (This is only a small portion of the database which contains multiple tables similar to this one being constructed)
create table Holidays (
staff_ID numeric(10) foreign key,
start_Date date,
fin_Date date,
holiday_Type char(100),
reason nvarchar(100),
);
The error I am receiving is:
Msg 102, Level 15, State 1, Line 2
Incorrect syntax near ','.
NOTE: I have created tables before in a similar fashion to this, and have looked over them to compare the differences (to which I can see is almost none).
Any help is much appreciated.
You have one "," extra on the end , also your foreign key mentioned badly.
Try this:
create table Holidays (
staff_ID numeric(10) foreign key references Table(Column),
start_Date date, fin_Date date, holiday_Type char(100),
reason nvarchar(100))
You have an extra comma after your last column definition. Try this:
create table Holidays (
staff_ID numeric(10) foreign key,
start_Date date,
fin_Date date,
holiday_Type char(100),
reason nvarchar(100) -- removed comma from here
);

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

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?