Insert data into two tables from single select statement in sql - sql

In my Sql server there is one database Employee which has table parish. and another one database with customer which has table clients and fips.There is one stored procedure which take data from this two tables clients and fips .
now i want to revert the operation. Take data from parish table and insert into clients and fips.
Below is the stored procedure which take data from clients and fips.
ALTER PROCEDURE [dbo].[Rpt_getexportparish] #clientID AS INT,
#assessment_type AS NVARCHAR(10),
#political_subDivision AS NVARCHAR(10),
#district AS NVARCHAR(10),
#acct_status AS NVARCHAR(10),
#millage_type AS NVARCHAR(10),
#tax_year AS NVARCHAR(10)
AS
BEGIN
SELECT FIPScode AS fips_code,
f.cnty_name AS gov_name,
c.NAME AS gov_agency,
PhysicalAddress1 AS address2,
PhysicalAddress2 AS address1,
TaxYear AS tax_year,
PhysicalAddressCity AS city,
PhysicalAddressState AS state,
PhysicalAddressZip AS zip,
AssessorName AS assr_name
FROM Clients c
JOIN fips f
ON c.FIPScode = f.cnty_fips
WHERE id = #clientID
AND place_fips = #political_subDivision
END
i want reverse of above. select data from parish and insert into clients and fips table. then what is the sql query for that.
There is no relation in both table clients and fips.

The OUTPUT command will allow you to insert into a secondary Table with a single query.
Here's an example of how it works
DROP TABLE #SOURCE
DROP TABLE #TARGET1
DROP TABLE #TARGET2
CREATE TABLE #SOURCE(ID INT IDENTITY(1,1),Val FLOAT)
GO
CREATE TABLE #TARGET1(ID INT,Val FLOAT)
GO
CREATE TABLE #TARGET2(ID INT,Val FLOAT)
GO
--ADD 10 ROWS TO SOURCE TABLE
INSERT INTO #SOURCE VALUES (RAND())
GO 10
INSERT INTO #TARGET1
OUTPUT INSERTED.* INTO #TARGET2
SELECT
*
FROM
#SOURCE

Related

Why do I get an invalid column name error when using a temp table twice?

If I run each of these batches separately, it works. However, if they are combined into one script (like what is done when a DACPAC script runs, or putting them both into one tab in SSMS), I get an Invalid column name error on the second insert. Why is that? If I need these to run in one script, do I need to use a different name for the temp table for the second batch? Or am I missing something that would allow me to use the same name?
IF OBJECT_ID('tempdb..#source') IS NOT NULL DROP TABLE #source
SELECT FirstName, LastName INTO #source FROM Musician WHERE 1 = 0; -- set up temp table schema
INSERT INTO #source ( FirstName, LastName )
VALUES
('Geddy', 'Lee'),
('Alex', 'Lifeson')
SELECT * FROM #source
GO
IF OBJECT_ID('tempdb..#source') IS NOT NULL DROP TABLE #source
SELECT [Name], Genre INTO #source FROM Band WHERE 1 = 0; -- set up temp table schema
INSERT INTO #source ( [Name], Genre )
VALUES
('Rush', 'Rock'),
('Ratt', 'Rock')
SELECT * FROM #source
GO
Each batch is parsed independently. So it works when you use GO because they are in different batches.
When you put everything in the same batch, SQL Server parses what it sees, and it is blind to logic like DROP commands hidden behind IF conditionals. Try the following and you'll find the same:
IF (1=0) DROP TABLE IF EXISTS #x; CREATE TABLE #x(i int);
IF (1=1) DROP TABLE IF EXISTS #x; CREATE TABLE #x(j date);
You and I both know that only one of those will ever execute, but the parser spots the redundant table name before it ever gets to execution (or evaluating any conditionals).
This works because, again, each batch is now parsed in isolation:
IF (1=0) DROP TABLE IF EXISTS #x; CREATE TABLE #x(i int);
GO
IF (1=1) DROP TABLE IF EXISTS #x; CREATE TABLE #x(j date);
This will in fact fail even though it passes parsing (highlight and select Parse instead of Execute), so the blindness goes both ways:
IF (1=0) DROP TABLE IF EXISTS #x; CREATE TABLE #x(i int);
GO
IF (1=1) CREATE TABLE #x(j date);
Using go after dropping the tables in both block will do the trick.
IF OBJECT_ID('tempdb..#source') IS NOT NULL DROP TABLE #source
go
SELECT FirstName, LastName INTO #source FROM Musician WHERE 1 = 0; -- set up temp table schema
INSERT INTO #source ( FirstName, LastName )
VALUES
('Geddy', 'Lee'),
('Alex', 'Lifeson')
SELECT * FROM #source
GO
IF OBJECT_ID('tempdb..#source') IS NOT NULL DROP TABLE #source
go
SELECT [Name], Genre INTO #source FROM Band WHERE 1 = 0; -- set up temp table schema
INSERT INTO #source ( [Name], Genre )
VALUES
('Rush', 'Rock'),
('Ratt', 'Rock')
SELECT * FROM #source
GO

how to move record from one database to another database

I have two database,where two table are same with all schema.
I want to move specific records of employees and employeesrates with all columns of both tables.
below is the query.
CREATE TABLE #emp
(
empID INT IDENTITY(1, 1) primary key ,
Firstname varchar(20)
);
CREATE TABLE #empRates
(
ID INT IDENTITY(1, 1) primary key ,
empid int, -- foreign key from #emp
rate decimal(10,3),
startdate datetime,
enddate datetime,
);
insert into #emp (firstname) values('First')
insert into #emp (firstname) values('Second')
insert into #emp (firstname) values('Third')
insert into #empRates(empid,rate,startdate,enddate) values(1,10,'2020/01/10','2020/01/20')
insert into #empRates(empid,rate,startdate,enddate) values(1,15,'2020/01/20','2020/01/30')
insert into #empRates(empid,rate,startdate,enddate) values(2,10,'2020/01/10','2020/01/20')
insert into #empRates(empid,rate,startdate,enddate) values(3,15,'2020/01/20','2020/01/30')
select * from #emp
select * from #empRates
drop table #emp
drop table #empRates
Here both database on same server. Database1 and Database2.
below my query which tried.
insert into database2..empRates(empid,rate,startdate,enddate) select empid,rate,startdate,enddate
from database1..empRates
Here my problem is both database have different records,so identity are different,so after insert other employee rates get displayed for another like mashed up.
I am using sql server 2012.
can you please provide the way.
You should take a look at this post --> How to turn IDENTITY_INSERT on and off using SQL Server 2008?
This way you can specify value for id column during insert, so rows on destination databases will keep IDs from origin.
Hope it helps!

Insert into SQL table with two foreign keys from Temporary table data

I have a table Hub:
and second table Rates:
In this, FromHubId and ToHubId are foreign keys from Hub table
I wanna add some data from a file in Rates table. What I have tried so far is:
Create a temp Table and insert values in it:
CREATE TABLE #Table
(FromHub varchar(30),
ToHub varchar(30),
Rate float,
rate40 float,
)
INSERT INTO #Table values('AUCKLAND','AUCKLAND',229.157325588818,341.973239724851),
('AUCKLAND','BLENHEIM',1825.03244430479,2738.13624934331),
('AUCKLAND','CHRISTCHURCH',1977.80399469734,2966.11840915988),
('AUCKLAND','DUNEDIN',2280.99676393793,3422.08272879301),
('AUCKLAND','GREYMOUTH',2432.59314855822,3650.06488860958),
('BLENHEIM','AUCKLAND',1520.66450929195,2280.99676393793),
('BLENHEIM','BLENHEIM',229.157325588818,341.973239724851),
('CHRISTCHURCH','AUCKLAND',1748.64666910852,2622.97000366278),
('CHRISTCHURCH','DUNEDIN',911.92863926627,1367.89295889941),
('CHRISTCHURCH','GREYMOUTH',685.121645221953,1028.27005071905),
:
:
:;
Create another Temp Table and trying to insert values in it:
CREATE TABLE #Table1
(FromHubId uniqueidentifier,
ToHubId uniqueidentifier,
Rate float,
Rate40 float,
FromHub varchar(30),
ToHub varchar(30)
);
insert into #Table1
select h.HubId As FromHubId, h.HubId As ToHubId, t.Rate, t.Rate40, t.FromHub, t.ToHub
FROM #Table t JOIN Hub h ON
t.FromHub=h.Centre and t.ToHub=h.Centre;
select * from #Table1;
But, It only insert values where FromHub and ToHub are same. I want to insert all values i.e. for different From and To hub as well.
Any help will be appreciated. Thanks..!!
TRY THIS: you have to join separately fro both from and to hubId using table alias as below:
INSERT INTO #Table1
SELECT DISTINCT h.HubId As FromHubId,
h1.HubId As ToHubId,
t.Rate,
t.Rate40,
t.FromHub,
t.ToHub
FROM #Table t
JOIN #Hub h ON t.FromHub=h.Centre
JOIN #Hub h1 ON t.ToHub=h1.Centre;

Select only few columns from procedure and insert into table

I have a stored procedure that returns 6 columns. But I want to take only 2 columns and insert them into my table variable.
DECLARE #CategoryTable TABLE(
CategoryId Int NOT NULL,
Name nvarchar(255) NOT NULL
)
INSERT INTO #CategoryTable EXEC [GetAllTenantCategories] #TenantId
When I run this
Column name or number of supplied values does not match table
definition
How to insert only specified columns from a stored procedure?
I do not want to use SELECT INTO as it is not supported by SQL Azure
Tried below and got Invalid object name '#Temp'
DECLARE #CategoryTable TABLE(
CategoryId Int NOT NULL,
Name nvarchar(255) NOT NULL
)
INSERT INTO #Temp EXEC [GetAllTenantCategories] 1
INSERT INTO #CategoryTable (CategoryId, Name)
SELECT CategoryId, Name from #Temp
DROP TABLE #Temp
You can create a temp table first and the INSERT the required columns in your table variable.
CREATE TABLE #temp
(
your columns and datatype
)
INSERT INTO #temp
EXEC [GetAllTenantCategories] #TenantId
Then you can,
DECLARE #CategoryTable TABLE(
CategoryId Int NOT NULL,
Name nvarchar(255) NOT NULL
)
INSERT INTO #CategoryTable (CategoryId, Name)
select CategoryId, Name from #temp
Also drop the #temp table,
DROP TABLE #temp
Refer the points taken from https://www.simple-talk.com/sql/performance/execution-plan-basics/
When the Estimated Plan is Invalid
In some instances, the estimated plan won't work at all. For example, try generating an estimated plan for this simple bit of code:
CREATE TABLE TempTable
(
Id INT IDENTITY (1 , 1 )
,Dsc NVARCHAR (50 )
);
INSERT INTO TempTable ( Dsc )
SELECT [Name]
FROM [Sales] .[Store] ;
SELECT *
FROM TempTable ;
DROP TABLE TempTable ;
You will get this error:
Invalid object name 'TempTable'.
The optimizer, which is what is used to generate Estimated Execution plans, doesn't execute T-SQL.
It does run the stateĀ­ments through the algebrizer , the process outlined earlier that is responsible for verifying the names of database objects. Since the query has not yet been executed, the temporary table does not yet exist. This is the cause of the error.
Running this same bit of code through the Actual execution plan will work perfectly fine.
Hope you got why your temp table approach not worked :) Because you might tried as T-SQL
We can use OPENQUERY
SELECT EmployeeID,CurrentSalary INTO #tempEmp
FROM OPENQUERY(LOCALSERVER,'Exec TestDB.dbo.spEmployee')

How do I insert from a table variable to a table with an identity column, while updating the the identity on the table variable?

I'm writing a SQL script to generate test data for our database. I'm generating the data in table variables (so I can track it later) and then inserting it into the real tables. The problem is, I need to track which rows I've added to the parent table, so that I can generate its child data later on in the script. For example:
CREATE TABLE Customer (
CustomerId INT IDENTITY,
Name VARCHAR(50)
)
CREATE TABLE Order (
OrderId INT IDENTITY,
CustomerId INT,
Product VARCHAR(50)
)
So, in my script, I create equivalent table variables:
DECLARE #Customer TABLE (
CustomerId INT IDENTITY,
Name VARCHAR(50)
) -- populate customers
DECLARE #Order TABLE (
OrderId INT IDENTITY,
CustomerId INT,
Product VARCHAR(50)
) -- populate orders
And I generate and insert sample data into each table variable.
Now, when I go to insert customers from my table variable into the real table, the CustomerId column in the table variable will become meaningless, as the real table has its own identity seed for its CustomerId column.
Is there a way I can track the new identity of each row inserted into the real table, in my table variable, so I can use a proper CustomerId for the order records? Or, is there a better way I should be going about this?
(Note: I originally started with an application to generate the test data, but it ran too slow during insert as > 1,000,000 records need to be generated.)
WHy do you need identity values on the table variables? If you use just int, you can isnert the ids after the insert is done. Grab them using the output clause. YOu might need an input values and an output values table varaiable to get this just right like this:
DECLARE #CustomerInputs TABLE (Name VARCHAR(50) )
DECLARE #CustomerOutputs TABLE (CustomerId INT ,Name VARCHAR(50) )
INSERT INTO CUSTOMERS (name)
OUTPUT inserted.Customerid, inserted.Name INTO #CustomerOutputs
SELECT Name FROM #CustomerInputs
SELECT * from #CustomerOutputs
You can insert the data to the table with a cursor and use the built-in function SCOPE_IDENTITY() to get the last id which was inserted in the current scope (by your script).
See this MSDN article for more information on SCOPE_IDENTITY.
Here is one way of doing it. If you can use it depends on your situation. You should not do it in production environment when users use your db.
-- Get the next identity values for Customer and Order
declare #NextCustomerID int
declare #NextOrderID int
set #NextCustomerID = IDENT_CURRENT('Customer')+1
set #NextOrderID = IDENT_CURRENT('Order')+1
-- Create tmp tables
create table #Customer (CustomerID int identity, Name varchar(50))
create table #Order (OrderID int identity, CustomerID int, Product varchar(50))
-- Reseed the identity columns in temp tables
dbcc checkident(#Customer, reseed, #NextCustomerID)
dbcc checkident(#Order, reseed, #NextOrderID)
-- Populate #Customer
-- Populate #Order
-- Allow insert to identity column on Customer
set identity_insert Customer on
-- Add rows to Customer
insert into Customer(CustomerId, Name)
select CustomerID, Name
from #Customer
-- Restore identity functionality on Customer
set identity_insert Customer off
-- Add rows to Order
set identity_insert [Order] on
insert into [Order](OrderID, CustomerID, Product)
select OrderID, CustomerID, Product
from #Order
set identity_insert [Order] off
-- Drop temp tables
drop table #Customer
drop table #Order
-- Check result
select * from [Order]
select * from Customer
The way I'd do it its first obtain the MAX(CustomerId) from your Customer Table. Then I'd get rid of the IDENTITY column on your variable table and do my own CustomerId using ROW_NUMBER() and the MaxCustomerId. It should be something like this:
DECLARE #MaxCustomerId INT
SELECT #MaxCustomerId = ISNULL(MAX(CustomerId),0)
FROM Customer
DECLARE #Customer TABLE (
CustomerId INT,
Name VARCHAR(50)
)
INSERT INTO #Customer(CustomerId, Name)
SELECT #MaxCustomerId + ROW_NUMBER() OVER(ORDER BY SomeColumn), Name
FROM YourDataTable
Or insert the values on a temp table, so you can use the same ids to fill your Order table.