I have the following SQL statement:
insert into [Order](UserId,MembershipType,PaymentAmt,PaymentStatus,StartDate,EndDate,Status)output INSERTED.OrderId values('18','Yearly','9.99','','2017-02-14 15:13:22','2018-02-14 15:13:22','1');Select Scope_Identity()
Which will insert the data in to my dbo.Order table in my Microsoft SQL Server 2014 database locally on my PC.
But when I run this query on a live 3rd party web server (With an identical database table) I get the following error:
exception=Cannot insert the value NULL into column 'OrderId', table 'db1128212_MYDATABASE.dbo.Order'; column does not allow nulls. INSERT fails.
Why is this?
Set the OrderId column on the remote table to identity/auto increment.
Auto increment primary key in SQL Server Management Studio 2012
Related
I have the same table in two different sql servers (one is SqlServer 2000 and the other 2008).
I'm using sql server management studio.
I want that each time an insert is made on a table in the SqlServer 2000 table (Table_1) a trigger occurs and the record would also be inserted to the same table in the SqlServer 2008 table (also Table_1).
the sql server 2008 is defined as a linked server, and it is possible to run queries and perform inserts on the 2008 db from the 2000 db connection using sql server management studio.
the trigger is defined as follows :
ALTER TRIGGER [dbo].[trgrTable]
ON [dbo].[Table_1]
AFTER INSERT
AS
BEGIN
INSERT INTO [TLVSQL].[AVI_DEV].[dbo].[Table_1](ID, Value)
SELECT INSERTED.ID AS ID, INSERTED.Value AS Value
FROM INSERTED
END
the [TLVSQL].[AVI_DEV] is the 2008 db name.
But every time i perform an insert on the 2000 table i get a message that the insert failed to commit due to "sqloledb was unable to begin a distributed transaction linked server ...".
The security delegation of the linked server is well defined, i explicitly set the security credentials to a user/password of a db_owner.
What am i doing wrong ? Is there another way of doing what i ask ?
thank you.
Perform inserts from a trigger into a table on linked server - bad decision.
This will great affects on insert performance in the source table ([dbo].[Table_1])
and also there is a distributed transaction, and configuring servers
for support distributed transactions - nightmare.
One possible solution is:
On the source server you can create a synchronization queue table. For example:
CREATE TABLE dbo.SyncQueue
(
QueueId INT IDENTITY(1,1),
KeyForSync INT, -- Primary key value of record in dbo.SourceTable
SyncStatus INT -- statuses can be: 0 - New, 1 - Synchronized, 2 - Error
)
suppose you source table is
CREATE TABLE dbo.SourceTable
(
Key INT, -- primary key of the table
Data varchar(xxx)
)
Triger on dbo.SourceTable can quickly insert into dbo.SyncQueue record Key that you need synchronize
Some periodically performed stored procedure can then insert records from the queue in
table on the linked server.
I am new to SQL Server and I wanted to create my first table there.
create table Employee
(
ID smallint not null
)
I use SQL Server 2008 R2 and Windows Authentication.
when I execute , it says :
CREATE TABLE permission denied in database 'master'.
Thanks!
Seems you're trying to create the table in master database where you may not have permission to create table. However, to create your target database please follow below steps:
a. At your SQLQuery editor choose your target database (Available Database drop down list) and execute your sql query.
Or
b. Try with below statement:
USE YourTargetDatabaseName
GO
CREATE TABLE Employee ( ID SMALLINT NOT NULL)
GO
I don't think you want to create a table in the master database.
Did you create a new database first? If so, use this:
USE [MyNewDatabaseName]
GO
create table Employee ( ID smallint not null )
GO
I have a SQL Server 2005. I have created a linked server to PG SQL server.
It works great.
SELECT *
FROM OpenQuery(POSTGRESQL_SERV,
'SELECT comp_id,comp_name FROM company WHERE comp_type = 5')
I need to read all data from PG SQL and insert it into SQL Server.
How to create a while ? (also unique id`s)
Thx.
Not sure if I understand your question entirely (while? unique id's?), but all you'd be doing would be an insert statement:
insert into MyTable ( comp_id, comp_name )
select comp_id, comp_name
from OpenQuery(POSTGRESQL_SERV,'SELECT comp_id,comp_name FROM company WHERE comp_type = 5')
As to creating unique ID's, you'd want to have an IDENTITY column on your destination table, which would create the ID's for you.
I'm looking for a solution to perform Insert, on duplicate key Update like operation in SQL Server 2005. This operation might Insert or Update large number of entries. SQL Server 2008 has a neat operation MERGE which would do it perfectly, the problem is we're stuck with SQL Server 2005.
I've looked into standard solutions, but all of them are not good, because they assume, that only one entry is updated/inserted.
Does anyone know a way to replicate MERGE behavior in older versions of SQL Server?
Alex Kuznetsov's blog contains a suggestion using the OUTPUT clause of an UPDATE statement. To paraphrase the example from that blog entry (untested):
DECLARE #updated_ids table(id int)
UPDATE table
SET ...
OUTPUT inserted.id INTO #updated_ids
FROM table INNER JOIN data-to-insert ON table.id = data-to-insert.id
INSERT INTO table
SELECT ...
FROM data-to-insert
WHERE id NOT IN (SELECT id FROM #updated_ids)
In sql server enterprise manager, how do you write an insert statement and pass in null values?
INSERT INTO atable (x,y,z) VALUES ( NULL,NULL,NULL)
If you're using SSMS (or old school Enterprise Manager) to edit the table directly, press CTRL+0 to add a null.