Hi I need to run this script to insert data from another server to my local sql server. How I can identify the path?
CMMS is the name of the table that is in my local sql. My pc name is itfg234. what should I replace for CMMS in this query.
SELECT * INTO CMMS
FROM (
SELECT N'178670' AS [_IdxIdentity], N'E94E6A98-B71A-41ED-8B4B-F6472BA72ECD' AS [Contract_Other_DateBooked] ) t;
GO
CREATE TABLE customer(
customer_id VARCHAR(10) NOT NULL PRIMARY KEY,
customer_name VARCHAR(20) NOT NULL,
address VARCHAR(40) NOT NULL
);
CREATE TABLE employee(
emp_id VARCHAR(10) NOT NULL PRIMARY KEY,
emp_name VARCHAR(30) NOT NULL,
contact_no VARCHAR(10) NOT NULL
);
I need insert data from customer to employees
INSERT customer SELECT customer_id, customer_name, address FROM employee;
If the destination table already exists then you can't use SELECT * INTO or you'll get error
There is already an object named 'CMMS' in the database.
SELECT INTO creates the destination table. Use INSERT INTO dbo.CMMS ([column names]) instead.
The query is cut off so I can't see the entire subquery but I'll just mention you should give it an alias or you may get
Incorrect syntax near ')'.
If you are planning to run the command on the source server I recommend that you design it to run on the destination server and select from source using a linked server or OPENROWSET function. It's easier to pull data than push it.
Hope this helps.
try this
CREATE TABLE new_table_name LIKE old_table_name;
INSERT new_table_name SELECT * FROM old_table_name;
hope this one help you.
Related
I have a utility script that is used to insert data into tables in my database. The script has a number of temp table in it that stores the new data to be inserted and a lot of it is related.
So, for example I have tables like so
DECLARE #Table1 TABLE
(
Table1ID INT
Table1Description VARCHAR(50)
Table1Code VARCHAR(5)
)
DECLARE #Table2 TABLE
(
Table2ID INT
Table2Description VARCHAR(50)
Table2Code VARCHAR(5)
)
DECLARE #Relationships TABLE
(
Table1Code VARCHAR(5)
Table2Code VARCHAR(5)
)
So the script populates the data in #Table1 and #Table2, but doesn't populate the ID fields. Once the data has been MERGEd into the database tables, I update the Table1ID and Table2ID fields in a separate statement as they are auto incrementing fields. Then when I use the #Relationships table to populate the database table, I can join to #Table1 and #Table2 to get the actual ID values.
I'm updating the script and I'm wondering if I can MERGE the data from #Table1/#Table2 into the database and update the ID fields in the temp table as part of the MERGE statement using the OUTPUT clause all in one statement?
I think the answer is no as I can't find anything mentioning updating an existing table with the OUTPUT clause, only inserting into a table.
I am still able to do what I need to do, so I'm not after alternatives. I just wondering if it is possible using the OUTPUT Clause
Thanks in advance
How can we completely replace the contents of a table with another table in a SQL Server database?
Like Truncate. We want to take all the data in one and put the data in the other one into it. How can we do? How do we do it with a script? By doing this automatically.
It happens in Oracle, does it happen in SQL Server? Thanks.
Just make simple insert into statement like this :
Also you can create procedure that will makes like just my simple example
DECLARE #employee1 TABLE(
Emp VARCHAR(100),
DOB datetime
)
INSERT INTO #employee1 SELECT 'ABC','1991-03-01'
INSERT INTO #employee1 SELECT 'XYZ','1992-12-01'
INSERT INTO #employee1 SELECT 'AJM','1992-08-20'
INSERT INTO #employee1 SELECT 'RNM','1991-07-10'
DECLARE #employee2 TABLE(
Emp VARCHAR(100),
DOB datetime
)
INSERT INTO #employee2
select * from #employee1
select '#employee1',* from #employee1
select '#employee2',* from #employee2
SQLFiddleDemo
If i am correct schema is same for both tables. In this case there are many way you can achieve this automatically,
Option 1: Call Stored Procedure from any web page or window service or any api.
Option 2: Create triggers.
In both way you can use following script to copy data from one table to other:
Truncate table <table1>
GO
INSERT <Table1> (column1, column2)
SELECT Column1, column2 FROM <Table2>
GO
In my SQL Server 2012 environment, I've created a series of stored procedures that pass pre-existing temporary tables among themselves (I have tried different architectures here, but wasn't able to bypass this due to the nature of the requirements / procedures).
What I'm trying to do is to, within a stored procedure check if a temporary table has already been created and, if not, to create it.
My current SQL looks as follows:
IF OBJECT_ID('tempdb..#MyTable') IS NULL
CREATE TABLE #MyTable
(
Col1 INT,
Col2 VARCHAR(10)
...
);
But when I try and run it when the table already exists, I get the error message
There is already an object named '#MyTable' in the database
So it seems it doesn't simply ignore those lines within the If statement.
Is there a way to accomplish this - create a temp table if it doesn't already exist, otherwise, use the one already in memory?
Thanks!
UPDATE:
For whatever reason, following #RaduGheorghiu's suggestion from the comments, I found out that the system creates a temporary table with a name along the lines of dbo.#MyTable________________________________________________0000000001B1
Is that why I can't find it? Is there any way to change that? This is new to me....
Following the link here, http://weblogs.sqlteam.com/mladenp/archive/2008/08/21/SQL-Server-2005-temporary-tables-bug-feature-or-expected-behavior.aspx
It seems as though you need to use the GO statement.
You meant to use IS NOT NULL i think... this is commonly used to clear temp tables so you don't get the error you mentioned in your OP.
IF OBJECT_ID('tempdb..#MyTable') IS NOT NULL DROP TABLE #MyTable
CREATE TABLE #MyTable
(
Col1 INT,
Col2 VARCHAR(10)
);
The big difference is the DROP TABLE statement after you do your logical check. Also, creating your table without filling data doesn't make it NULL
DROP TABLE #MyTable
CREATE TABLE #MyTable
(
Col1 INT,
Col2 VARCHAR(10)
);
IF OBJECT_ID('tempdb..#MyTable') IS NOT NULL
SELECT 1
Try wrapping your actions in a begin...end block:
if object_id('tempdb..#MyTable') is null
begin
create table #MyTable (
Col1 int
, Col2 varchar(10)
);
end
This seems odd, but it works when I try it
IF(OBJECT_ID('tempdb..#Test') IS NULL) --check if it exists
BEGIN
IF(1 = 0)--this will never actually run, but it tricks the parser into allowing the CREATE to run
DROP TABLE #Test;
PRINT 'Create table';
CREATE TABLE #Test
(
ID INT NOT NULL PRIMARY KEY
);
END
IF(NOT EXISTS(SELECT 1 FROM #Test))
INSERT INTO #Test(ID)
VALUES(1);
SELECT *
FROM #Test;
--Try dropping the table and test again
--DROP TABLE #Test;
I've searched and found this article about temporary tables in SQL Server because I've met a line in one of our stored procedures saying:
SELECT Value SomeId INTO #SomeTable FROM [dbo].[SplitIds](#SomeIds, ';')
I know that #SomeTable is stored in tempdb as a temporary table. However, I don't understand why we don't have to use CREATE TABLE #SomeTable first as it is written in the mentioned article. Our code is working fine, I just don't get why it is enough to use SELECT ... INTO #SomeTable. What would be the consequence when I add CREATE TABLE #SomeTable at the beginning? Would we get any differences in performance? Would the table be stored at another location?
Select ... into [table] uses the properties of the dataset generated from the Select statement to create a temporary table and subsequently fill the table.
The alternative to using Select ... into [table] is to use a Create Table statement followed by an Insert Into statement. Explicitly creating the table offers more control and precision.
Using a Select ... into [Table] may seem like a no-brainer, but there are situations where Select ... into [Table] can be problematic.
For instance, when you are going to create a temporary table and insert additional rows at a later time, using the Select ... into [Table] syntax can cause problems, especially with string-based and nullable fields.
As an example of the limitations of the Select ... into [table], the script below creates a temporary table with two fields, First_Name and Last_Name. Next, an Insert statement attempts to add another record to the temporary table, but fails as the values would be truncated.
Select 'Bob' as First_Name
, 'Smith' as Last_Name
Into #tempTable;
Insert into #tempTable (First_Name, Last_Name)
Select 'Christopher' as First_Name
, 'Brown' as Last_Name;
The script fails because the Select ... into [table] statement creates a table equivalent to the following script:
Create Table #tempTable (
First_Name varchar(3) Not Null
Last_Name varchar(5) Not Null
);
I am copying a table from one database to another, actually from one connection to another. The problem is that the key field is an identity field. In the new database in does not the identity property.
So when I try to insert into the new table I get an error because the field which used to have an identity property cannot be null. I could create a new identity field and then rename it to the original name but then the values would be out of sync with the other tables it is link to.
Thanks in advance for any help.
Bob
The answer can be found in the second comment. I'll paste it here:
CREATE TABLE dbo.Tmp_Names
(
Id int NOT NULL
IDENTITY(1, 1),
Name varchar(50) NULL
)
ON [PRIMARY]
go
SET IDENTITY_INSERT dbo.Tmp_Names ON
go
IF EXISTS ( SELECT *
FROM dbo.Names )
INSERT INTO dbo.Tmp_Names ( Id, Name )
SELECT Id,
Name
FROM dbo.Names TABLOCKX
go
SET IDENTITY_INSERT dbo.Tmp_Names OFF
go
This will create table [db2].[dbo].[YoutableNameYouWantInDb2] in db2 with same structure as in db1 and copy all data from [db1].[dbo].[yourTableName].
SELECT * INTO [db2].[dbo].[YoutableNameYouWantInDb2]
FROM [db1].[dbo].[yourTableName]