Ambiguous column name... but only sometimes? - sql

So apparently this has Ambiguous column name 'LocationID':
DECLARE #temptable table (LocationID int)
INSERT INTO #temptable SELECT LocationID FROM inserted;
INSERT INTO dbo.LocationsPlants (LocationID, PlantID)
(SELECT LocationID, PlantID FROM dbo.Plants CROSS JOIN #temptable)
Which I can fix by altering the bottom line to:
(SELECT T.LocationID, PlantID FROM dbo.Plants CROSS JOIN #temptable AS T)
But this identical query on another table does NOT have ambiguous column IncotermsID:
DECLARE #temptable table (IncotermsID int)
INSERT INTO #temptable SELECT IncotermsID FROM inserted;
INSERT INTO dbo.IncotermsPlants (IncotermsID, PlantID)
(SELECT IncotermsID,PlantID FROM dbo.Plants CROSS JOIN #temptable)
I'm puzzled. Table structures:
dbo.Locations:
[LocationID] [int] NOT NULL,
[LocationTypeID] [int] NOT NULL,
[Title] [varchar](100) NULL
dbo.Incoterms:
[IncotermsID] [int] IDENTITY(1,1) NOT NULL,
[Incoterm] [varchar](20) NOT NULL

The problem is most likely a column named LocationID in the Plants table, which is why the query is confused as to which LocationID column to be returned, from Plants or from #temptable?
As #GordonLinoff mentioned, it's a good practice (I'd say best practice) to always alias your tables used in joins or correlated queries, and do so for their associated columns as well.
The reason this only happens "sometimes" is because in your second query, there is a single IncotermsID exists in only one table of the two used in your CROSS APPLY.

Related

How to insert data in multiple tables using single query in SQL Server?

I'm trying to insert data into multiple tables if it doesn't already exist. I can't seem to figure this out at all.
Table 1:
CREATE TABLE [dbo].[search_results]
(
[company_id] [int] NULL,
[title] [text] NULL,
[link] [text] NULL,
[domain] [text] NULL,
[index] [int] NULL,
[id] [int] PRIMARY KEY IDENTITY(1,1) NOT NULL
)
Table 2:
CREATE TABLE [dbo].[statements]
(
[statement_link_id] [int] NULL,
[statement_page] [text] NULL,
[statement_text_location] [text] NULL,
[statement_description] [text] NULL,
[statement_description_html] [text] NULL,
[statement] [int] NULL,
[id] [int] PRIMARY KEY IDENTITY(1,1) NOT NULL
)
This is what I want to do:
check to see if the company_id and the link already exist in the table or not.
SELECT *
FROM search_results
WHERE company_id = 4 AND link = 'https://test.com';
If the data does not exist, insert it into two tables
INSERT INTO search_results (company_id, link, title, domain)
VALUES (4, 'https://test.com', 'title', 'test.com');
and also insert the search_result last inserted id to the following table. corporate_statement value is always 1
INSERT INTO corporate_statements (statement_link_id, corporate_statement)
VALUES (743, 1);
I'm trying this based on what I found on SO
DECLARE #result AS TABLE (id int, company_id int, link text, title text, domain text);
WITH cte AS
(
SELECT *
FROM (VALUES (4, 'https://test.com', null, null)) AS t(company_id, link, title, domain)
)
INSERT INTO #result
SELECT *
FROM
(INSERT INTO dbo.search_results (company_id, link, title, domain)
OUTPUT inserted.*
SELECT * FROM cte
WHERE NOT EXISTS (SELECT * FROM dbo.[search_results]
WHERE company_id = cte.company_id
AND CAST(link AS varchar(250)) = CAST(cte.link AS varchar(50))
)) r
SELECT * FROM #result;
Even trying with a single insert statement, I get the following error:
Msg 213, Level 16, State 1, Line 8
Column name or number of supplied values does not match table definition.
As you can see, I also tried to cast it to varchar since it was throwing error when I hadn't. How can update this?
To me - this seems a lot cleaner, and it also will be a lot simpler to understand (and maintain!) in the future:
-- check to see if your data already exists
IF NOT EXISTS (SELECT *
FROM search_results
WHERE company_id = 4 AND link = 'https://test.com')
BEGIN TRY
BEGIN TRANSACTION
-- if not -> insert into the first table
INSERT INTO search_results (company_id, link, title, domain)
VALUES (4, 'https://test.com', 'title', 'test.com');
-- grab the last identity value from that previous INSERT
DECLARE #LastId INT;
SELECT #LastId = SCOPE_IDENTITY();
-- insert into the second table
INSERT INTO corporate_statements (statement_link_id, corporate_statement)
VALUES (#LastId, 1);
COMMIT;
END TRY
BEGIN CATCH
-- in case of an error rollback the full transaction
ROLLBACK;
END CATCH;
and you're done. Or am I missing something? I think this would be doing what you're described in the intro of your post - not necessarily what you're showing in your code...

Insert data from user-defined table on some fields, other fields must use scalar variable

How would I make use of my user-defined table that comes loaded with data, insert into a table but 1 of the table fields won't be using the data from the User Defined Table. I want a scalar variable to take its place.
#dataTableType is the following user-defined table:
CREATE TYPE [dbo].[NewSequenceType] AS TABLE (
[FK_Sequence] [int] NULL,
[FK_Status] [int] NULL,
[FK_Shift] [int] NULL,
[PK_SequenceType] [int] NULL,
[TypeName] [varchar](30) NULL,
[SequenceName] [varchar](100) NULL,
[SequenceDetails] [varchar](400) NULL,
[SequenceStatus] [varchar](15) NULL,
[SequenceShift] [varchar](15) NULL,
[EmployeeId] [varchar](8) NULL,
[EquipmentId] [varchar](25) NULL,
[Comments] [varchar](512) NULL,
[EnteredDate] [datetime] NULL
)
My stored procedure:
ALTER PROCEDURE [dbo].[spNewInspectionEntry] (#dataTableType NewSequenceType READONLY)
INSERT INTO dbo.PIT_Inspection (FK_Sequence, FK_Status, FK_Shift, FK_EmployeeName, FK_EquipmentName, Comments, EnteredDate)
SELECT
FK_Sequence, FK_Status, FK_Shift, EmployeeId,
EquipmentId, Comments, EnteredDate
FROM
#dataTableType;
What I'm looking to do:
DECLARE #EmployeeIDExists varchar(8);
--Note that I only care about one row, this is Ok.
SELECT TOP 1 #EmployeeIDExists = EmployeeId FROM #dataTableType;
--Retrieve PKEmployeeId
SELECT #EmployeeIdPK = PK_EmployeeName
FROM dbo.PIT_EmployeeName
WHERE EmployeeId = #EmployeeIDExists
--Now I need to insert the value of #EmployeeIdPK? Along with the rest, it needs to replace EmployeeID and EquipmentId, Not sure how to...
INSERT INTO dbo.PIT_Inspection (FK_Sequence, FK_Status, FK_Shift, FK_EmployeeName, FK_EquipmentName, Comments, EnteredDate)
SELECT
FK_Sequence, FK_Status, FK_Shift, EmployeeId,
EquipmentId, Comments, EnteredDate
FROM
#dataTableType;
How can I get #EmployeeIdPK in Along with the rest, it needs to replace EmployeeID, I don't want the EmployeeId from the user-defined table to go there, I want the variable #EmployeeIdPK instead.
You can put any expression, variable, and literal value in the SELECT list. Just keep in mind that those values will be repeated for all rows.
Meaning:
SELECT Field1,
Field2,
'hello' AS [LiteralText],
5 AS [LiteralNumber],
#Variable AS [ValueFromVariable]
FROM TableName;
will repeated those literal and variable values for all rows.
Hence:
INSERT INTO dbo.PIT_Inspection (FK_Sequence, FK_Status, FK_Shift, FK_EmployeeName,
FK_EquipmentName, Comments, EnteredDate)
SELECT
FK_Sequence, FK_Status, FK_Shift, #EmployeeIdPK,
EquipmentId, Comments, EnteredDate
FROM
#dataTableType;

What's the query needed to copy data from one table to another two?

I recently changed my database structure and now I want to copy from my old table Transfers to the new ones I just created (Orders and Orders_Transfer):
--old table to copy from
-- table 'Transfers'
CREATE TABLE [dbo].[Transfers] (
[Id] int IDENTITY(1,1) NOT NULL,
[Date] datetime NOT NULL,
[Memo] nvarchar(max) NULL,
[Employee_Id] int NULL,
[InventoryFrom_Id] int NOT NULL,
[InventoryTo_Id] int NOT NULL,
);
-- new tables to copy to
-- table 'Orders'
CREATE TABLE [dbo].[Orders] (
[Id] int IDENTITY(1,1) NOT NULL,
[Date] datetime NOT NULL,
[Memo] nvarchar(max) NULL,
[Employee_Id] int NULL
);
-- Creating table 'Orders_Transfer'
CREATE TABLE [dbo].[Orders_Transfer] (
[Id] int NOT NULL,--foreign key on Orders.Id
[InventoryFrom_Id] int NOT NULL,
[InventoryTo_Id] int NOT NULL
);
I want to iterate through the old Transfers table and copy some part of it to Orders (Date, Memo, Employee) and the rest to Orders_Transfer (InventoryFrom_Id, InventoryTo_Id). The Id column in Orders_Transfer is also a FK on Orders.Id so I want to copy the auto-generated value as well.
I read about the scope_identity() function and the OUTPUT clause, but I’m a beginner to SQL and can’t put it all together.
I’m using SQL Server 2008
What is the query I need to do this? Any help would be appreciated, thanks!
I would create an OldId column on the Orders table to store the old primary key:
ALTER TABLE [dbo].[Orders] ADD [OldId] INT;
Then copy in the old data:
INSERT INTO [dbo].[Orders]
(
[OldId],[Date],[Memo],[EmployeeID]
)
SELECT [Id] AS [OldId],[Date],[Memo],[EmployeeID]
FROM [dbo].[Transfers];
Copy the remaining data using the OldId:
INSERT INTO [dbo].[Orders_Transfer]
(
[Id],
[InventoryFrom_Id],
[InventoryTo_Id]
)
SELECT
O.Id, T.[InventoryFrom_Id], T.[InventoryTo_Id]
FROM [dbo].[Orders] O
INNER JOIN [dbo].[Transfers] T
ON O.[OldId] = T.[Id];
And drop the OldId column:
ALTER TABLE [dbo].[Orders] DROP COLUMN [OldId];
To keep the Id values in sync you are going to need to use IDENTITY_INSERT.
SET IDENTITY_INSERT dbo.Orders ON;
/* Insert data into the Orders table */
INSERT INTO [dbo].[Orders]
([Id]
,[Date]
,[Memo]
,[Employee_Id])
SELECT Id
, Date
, Memo
, Employee_Id
FROM Transfers;
SET IDENTITY_INSERT dbo.Orders OFF;
/* Insert data into the Orders_Transfer table */
INSERT INTO [dbo].[Ordres_Transfer]
([Id]
,[InventoryFrom_ID]
,[InventoryTo_ID]
SELECT Id
, InventoryFrom_ID
, InentoryTo_ID
FROM Transfers;
Insert into newtable select * from oldtable //if same schema
insert into newtable(col1,col2,col3) select col1,col2,col3 from old table // for different table schema

Subquery Performance - Non Unique Column in Where Clause

I have two tables
Table Jobs
[ID] [int] IDENTITY(1,1) NOT NULL,
[title] [varchar](150) NULL,
[description] [text] NULL
Table JobSkills
[id] [int] IDENTITY(1,1) NOT NULL,
[jobId] [int] NULL,
[skill] [varchar](150) NULL
Shown above partial list of columns.
For table JobSkills I have indexed jobId column, column skill is full text indexed.
I have a stored procedure to get the list of jobs. sort of like this.
Select totalItems
,Id,title
from
(
Select Row_Number() over(Order By
CASE WHEN #sortBy Is Not Null AND #sortBy='relevance'
THEN
SkillMatchRank
END DESC
,CASE WHEN #sortBy Is Not Null AND #sortBy='date' THEN CreateDate END DESC
) As rowNumber
,COUNT(*) OVER() as totalItems
,ID,createDate,title
from Jobs J
OUTER APPLY dbo.GetJobSkillMatchRank(J.ID,#searchKey) As SkillMatchRank
Where
--where conditions here
) tempData
where
rowNumber>=CASE WHEN #startIndex>0 AND #endIndex>0 THEN #startIndex ELSE rowNumber END
AND rowNumber<=CASE WHEN #startIndex>0 AND #endIndex>0 THEN #endIndex ELSE rowNumber END
I have created a inline table valued function to get the skill matching rank.
CREATE FUNCTION [dbo].[GetJobSkillMatchRank]
(
#jobId int,
#searchKey varchar(150)
)
RETURNS TABLE
AS
RETURN
(
select SUM(ISNULL(JS2.[Rank],0)) as rank
from FREETEXTTABLE(JobSkills,skill,#searchKey) JS2
Where JS2.[Key] in (Select ID from JobSkills Where jobId=#jobId)
)
GO
Problem
Query runs super slow, more then a minute.
My observation
For the table valued function if I set jobId=1 (I do have a job with id=1) then it performs super fast as desired.
I understand that jobId is not unique column on JobSkills table.
In this case how could I improve the performance???
UDFs are great in certain cases, but the execution plans don't get cached like sprocs do. If you try using another derived table instead of a function, the query might perform better.

How can I INSERT data into two tables simultaneously in SQL Server?

Let's say my table structure looks something like this:
CREATE TABLE [dbo].[table1] (
[id] [int] IDENTITY(1,1) NOT NULL,
[data] [varchar](255) NOT NULL,
CONSTRAINT [PK_table1] PRIMARY KEY CLUSTERED ([id] ASC)
)
CREATE TABLE [dbo].[table2] (
[id] [int] IDENTITY(1,1) NOT NULL,
[table1_id] [int] NOT NULL,
[data] [varchar](255) NOT NULL,
CONSTRAINT [PK_table2] PRIMARY KEY CLUSTERED ([id] ASC)
)
The [id] field of the first table corresponds to the [table1_id] field of the second. What I would like to do is insert data into both tables in a single transaction. Now I already know how to do this by doing INSERT-SELECT-INSERT, like this:
BEGIN TRANSACTION;
DECLARE #id [int];
INSERT INTO [table1] ([data]) VALUES ('row 1');
SELECT #id = SCOPE_IDENTITY();
INSERT INTO [table2] ([table1_id], [data]) VALUES (#id, 'more of row 1');
COMMIT TRANSACTION;
That's all good and fine for small cases like that where you're only inserting maybe a handful of rows. But what I need to do is insert a couple hundred thousand rows, or possibly even a million rows, all at once. The data is coming from another table, so if I was only inserting it into a single table, it would be easy, I'd just have to do this:
INSERT INTO [table] ([data])
SELECT [data] FROM [external_table];
But how would I do this and split the data into [table1] and [table2], and still update [table2] with the appropriate [table1_id] as I'm doing it? Is that even possible?
Try this:
insert into [table] ([data])
output inserted.id, inserted.data into table2
select [data] from [external_table]
UPDATE: Re:
Denis - this seems very close to what I want to do, but perhaps you could fix the following SQL statement for me? Basically the [data] in [table1] and the [data] in [table2] represent two different/distinct columns from [external_table]. The statement you posted above only works when you want the [data] columns to be the same.
INSERT INTO [table1] ([data])
OUTPUT [inserted].[id], [external_table].[col2]
INTO [table2] SELECT [col1]
FROM [external_table]
It's impossible to output external columns in an insert statement, so I think you could do something like this
merge into [table1] as t
using [external_table] as s
on 1=0 --modify this predicate as necessary
when not matched then insert (data)
values (s.[col1])
output inserted.id, s.[col2] into [table2]
;
I was also struggling with this problem, and find that the best way is to use a CURSOR.
I have tried Denis solution with OUTPUT, but as he mentiond, it's impossible to output external columns in an insert statement, and the MERGE can't work when insert multiple rows by select.
So, i've used a CURSOR, for each row in the outer table, i've done a INSERT, then use the ##IDENTITY for another INSERT.
DECLARE #OuterID int
DECLARE MY_CURSOR CURSOR
LOCAL STATIC READ_ONLY FORWARD_ONLY
FOR
SELECT ID FROM [external_Table]
OPEN MY_CURSOR
FETCH NEXT FROM MY_CURSOR INTO #OuterID
WHILE ##FETCH_STATUS = 0
BEGIN
INSERT INTO [Table] (data)
SELECT data
FROM [external_Table] where ID = #OuterID
INSERT INTO [second_table] (FK,OuterID)
VALUES(#OuterID,##identity)
FETCH NEXT FROM MY_CURSOR INTO #OuterID
END
CLOSE MY_CURSOR
DEALLOCATE MY_CURSOR
Keep a look out for SQL Server to support the 'INSERT ALL' Statement. Oracle has it already, it looks like this (SQL Cookbook):
insert all
when loc in ('NEW YORK', 'BOSTON') THEN
into dept_east(deptno, dname, loc) values(deptno, dname, loc)
when loc in ('CHICAGO') THEN
into dept_mid(deptno, dname, loc) values(deptno, dname, loc)
else
into dept_west(deptno, dname, loc) values(deptno, dname, loc)
select deptno, dname, loc
from dept
BEGIN TRANSACTION;
DECLARE #tblMapping table(sourceid int, destid int)
INSERT INTO [table1] ([data])
OUTPUT source.id, new.id
Select [data] from [external_table] source;
INSERT INTO [table2] ([table1_id], [data])
Select map.destid, source.[more data]
from [external_table] source
inner join #tblMapping map on source.id=map.sourceid;
COMMIT TRANSACTION;
Create table #temp1
(
id int identity(1,1),
name varchar(50),
profession varchar(50)
)
Create table #temp2
(
id int identity(1,1),
name varchar(50),
profession varchar(50)
)
-----main query ------
insert into #temp1(name,profession)
output inserted.name,inserted.profession into #temp2
select 'Shekhar','IT'
You could write a stored procedure that iterates over the transaction that you have proposed. The iterator would be the cursor for the table that contains the source data.
Another option is to run the two inserts separately, leaving the FK column null, then running an update to poulate it correctly.
If there is nothing natural stored within the two tables that match from one record to another (likely) then create a temporary GUID column and populate this in your data and insert to both fields. Then you can update with the proper FK and null out the GUIDs.
E.g.:
CREATE TABLE [dbo].[table1] (
[id] [int] IDENTITY(1,1) NOT NULL,
[data] [varchar](255) NOT NULL,
CONSTRAINT [PK_table1] PRIMARY KEY CLUSTERED ([id] ASC),
JoinGuid UniqueIdentifier NULL
)
CREATE TABLE [dbo].[table2] (
[id] [int] IDENTITY(1,1) NOT NULL,
[table1_id] [int] NULL,
[data] [varchar](255) NOT NULL,
CONSTRAINT [PK_table2] PRIMARY KEY CLUSTERED ([id] ASC),
JoinGuid UniqueIdentifier NULL
)
INSERT INTO Table1....
INSERT INTO Table2....
UPDATE b
SET table1_id = a.id
FROM Table1 a
JOIN Table2 b on a.JoinGuid = b.JoinGuid
WHERE b.table1_id IS NULL
UPDATE Table1 SET JoinGuid = NULL
UPDATE Table2 SET JoinGuid = NULL