This question already has an answer here:
How can I implement this plan in C# with SQL command?
(1 answer)
Closed 7 years ago.
I'm write this query:
INSERT INTO Customer_Care_Database_Analysis_Center
SELECT code_markaz, name_markaz
FROM Customer_Care_Database_Analysis_TEMP
WHERE NOT EXISTS (SELECT 1
FROM Customer_Care_Database_Analysis_TEMP ti
WHERE ti.code_markaz = Customer_Care_Database_Analysis_Center.code_markaz);
but when I run that query I get this error:
Msg 4104, Level 16, State 1, Line 6
The multi-part identifier "Customer_Care_Database_Analysis_Center.code_markaz" could not be bound.
What happened? How can I solve that?
Not sure what's happening with the answers, but I would assume you want to insert the rows that don't exist in the table (field code_markaz) and that you can do this way:
INSERT INTO Customer_Care_Database_Analysis_Center (code_markaz, name_markaz)
SELECT code_markaz, name_markaz
FROM Customer_Care_Database_Analysis_TEMP tmp
WHERE NOT EXISTS (SELECT 1
FROM Customer_Care_Database_Analysis_Center c
WHERE c.code_markaz = tmp.code_markaz)
I also added the columns in the insert statement. If there's only 2 columns in the table it will work without them.
The table being inserted into (Customer_Care_Database_Analysis_Center) does not have a column called code_markaz.
Name the target columns:
INSERT INTO Customer_Care_Database_Analysis_Center (some_col_1, some_col_2)
SELECT code_markaz, name_markaz
FROM ...
The error comes due to Customer_Care_Database_Analysis_Center table not reference in you not exist inner query.
As above suggested you have to give either alias or join that table.
...FROM Customer_Care_Database_Analysis_TEMP t
JOIN Customer_Care_Database_Analysis_Center ccdac
NOT EXISTS (
SELECT 1
FROM Customer_Care_Database_Analysis_TEMP ti
WHERE ti.code_markaz = CCDAC.code_markaz);
Related
This question already has answers here:
Using IF EXISTS with a CTE
(5 answers)
Closed 5 years ago.
I have a table-valued function that selects rows as part of a CTE, and I then want to perform a different select/insert into the returned table if no rows were selected in the CTE:
WITH myCte AS (SELECT * FROM abc WHERE xyz = 123)
IF NOT EXISTS (SELECT * FROM myCte)
BEGIN
...
END
However I'm getting a syntax error whenever I go to use CTE tables in the NOT EXISTS condition. My myCte is a lot more complex than the example, so it will be awkward to place the whole query in the condition.
How can I use the CTE result in the condition check?
You need to declare your common table expression in the context where it will be used and it can only be used for the statement that follows with. If you want to use the results multiple times, you can either repeat the common table expression multiple times, or you can use a table variable or temporary table to hold the results instead.
WITH myCte AS (SELECT * FROM abc WHERE xyz = 123)
select *
into #myTempTable
from myCte
IF NOT EXISTS (SELECT * FROM #myTempTable)
BEGIN
...
END
This question already has answers here:
sql server 2008 management studio not checking the syntax of my query
(2 answers)
Closed 6 years ago.
I was purging some invalid information from a database and ran the following query
delete from table1 where
table1id in( select table1id from wrongTable )
and
table1id not in (select validColumn from table3)
And it deleted several thousand rows.
Then I executed just :
select table1id from wrongTable
and got the error message:
Msg 207, Level 16, State 1, Line 1
Invalid column name 'table1id'
I then realized my error. My IN clause should have been
select fktable1id from correctTable
Luckily, I had the second clause. Otherwise, I would have completely wiped out the table.
Am I missing something or is this a bug? Shouldn't the first query have failed to execute?
Here is script to reproduce:
USE [SynDoc_Demo]
GO
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[temp1]') AND type in (N'U'))
DROP TABLE [dbo].[temp1]
GO
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[rightTable]') AND type in (N'U'))
DROP TABLE [dbo].[rightTable]
GO
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[wrongTable]') AND type in (N'U'))
DROP TABLE [dbo].[wrongTable]
GO
create table temp1 (itemid int, name varchar(25))
go
insert into temp1 values (1,'bob')
insert into temp1 values (2,'dave')
go
create table wrongTable (junk int)
go
insert into wrongTable values (10)
go
create table rightTable (fkitemid int)
go
insert into rightTable values (2)
go
select * from temp1
go
delete from temp1 where itemid in (select itemid from wrongTable)
go
select * from temp1
EDIT:
Edited sample query to clarify for anyone attempting to reproduce
Not an answer but too long winded for the short comments.
Like Giorgos I couldn't reproduce until you added your script.
If I change the delete to
SELECT * FROM TEMP1 WHERE ITEMID IN (SELECT ITEMID FROM WRONGTABLE)
it still returns the two rows. Suspecting some kind of assumption by the engine that because ITEMID exists in TEMP1, tried to qualify it further
the following fails.
SELECT * FROM TEMP1 WHERE ITEMID IN (SELECT A.ITEMID FROM WRONGTABLE AS A)
Still - it's a great spot and one to be wary of.
I'm beginner in write query,i write this query for insert any data to table:
insert into ScoreTable (UserID,Score,PhoneNumber,date_)
select id,sum(Duration) / 60 ,phoneNumber,Date_ from CDR p
where p.phoneNumber=phoneNumber
but when i execute the that query,i get this error:
Msg 8120, Level 16, State 1, Line 27
Column 'CDR.id' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
How can i solve that problem?
Your issue is with the select statement. Run that independently and you will get the same issue. Because you're aggregating (SUM) you require a group by clause. Give this a go;
SELECT p.id
,sum(p.Duration) / 60
,p.phoneNumber
,p.Date_
FROM CDR p
WHERE p.phoneNumber = phoneNumber
GROUP BY p.id, p.phoneNumber, p.Date
Also remember to use your table aliases in the queries to avoid ambiguity.
I'm trying to insert new_file_name column data from document_image_volume1_rename_temp table into the document_image_volume1_rename table. Both tables have a column document_image_id.
USE myDatabase
INSERT INTO document_image_volume1_rename (new_file_name)
SELECT
new_file_name
FROM
document_image_volume1_rename_temp
WHERE
document_image_volume1_rename.document_image_id = document_image_volume1_rename_temp.document_image_id
Here's the error message:
Msg 4104, Level 16, State 1, Line 10
The multi-part identifier "document_image_volume1_rename.document_image_id" could not be bound.
I think what you are actually looking for is an UPDATE query not an INSERT query. If you don't want to add new rows but just modify a column in existing rows, try this:
UPDATE t2
SET t2.new_file_name = t1.new_file_name
FROM document_image_volume1_rename_temp t1 INNER JOIN document_image_volume1_rename t2 ON t1.document_image_id = t2.document_image_id
A handy reference on UPDATE query syntax by DBMS can be found in Eric's answer here.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
sql server 2008 management studio not checking the syntax of my query
I ran across an issue today where a subquery was bad and the result was all rows from the parent table were deleted.
TableA
ID,
Text,
GUID
TableB
ID,
TableAID,
Text
delete from TableB
where id in (
select TableAID
from TableA
where GUID = 'fdjkhflafdhf'
)
If you run the subquery by itself you get an error since the column (TableAID) doesn't exist in Table A. If you run the full query - it deletes all records from table B without an error.
I also tried the following queries which removed 0 records (expected)
delete from TableB where id in (null)
delete from TableB where id in (select null)
Can someone explain to my why this is occurring when the query is malformed? Why does it seem to evaluate to true?
Note: This was tested on SQL Server 2008 R2
As TableAID doesn't exist in TableA, the query is using the column from TableB. Therefore the query is the same as:
delete from TableB
where id in (
select TableB.TableAID
from TableA
where GUID = 'fdjkhflafdhf'
)
So in essence it's doing:
delete from TableB
where id in (TableAID)
If you are using sub-queries its best to mention your table names when referencing. The following WILL throw an exception:
delete from TableB
where id in (
select TableA.TableAID
from TableA
where TableA.GUID = 'fdjkhflafdhf'
)
Furthermore I would use an alias so that we know which query we are referring to:
delete from TableB
where id in (
select a.TableAID
from TableA a
where a.GUID = 'fdjkhflafdhf'
)