October 7, 2012 at 8:01 am
hay....
I want insert to table (table name is: "portfolioForExpert") 2 values:
1) expertID
2) portfolioID (value of "1" represent: "stocks", value of "2" represent" "bonds")
but I want it only if the combination of these two value does'nt exists in the table yet...
so I try this command: (by the way I use webMatrix+asp.net so "INSERT IGNORE" will not work...)
updateQuery="INSERT INTO portfolioForExpert(ExpertID,portfolioID)VALUES(@0,'2')WHERE NOT EXISTS(SELECT * FROM portfolioForExpert WHERE ExpertID=@0 AND portfolioID='2')";
db.Execute(updateQuery,ExpertToEdit.ToString());
but I get error like this:
"There was an error parsing the query. [ Token line number = 1,Token line offset = 67,Token in error = WHERE]" (I think its means about the first WHERE....But I don't sure..)
I try everything but I do'nt find a problem with that...
could somebody help me please?
Thanks in advance!!
ofir.
October 7, 2012 at 8:43 am
ofirhgy (10/7/2012)
hay....I want insert to table (table name is: "portfolioForExpert") 2 values:
1) expertID
2) portfolioID (value of "1" represent: "stocks", value of "2" represent" "bonds")
but I want it only if the combination of these two value does'nt exists in the table yet...
so I try this command: (by the way I use webMatrix+asp.net so "INSERT IGNORE" will not work...)
updateQuery="INSERT INTO portfolioForExpert(ExpertID,portfolioID)VALUES(@0,'2')WHERE NOT EXISTS(SELECT * FROM portfolioForExpert WHERE ExpertID=@0 AND portfolioID='2')";
db.Execute(updateQuery,ExpertToEdit.ToString());
but I get error like this:
"There was an error parsing the query. [ Token line number = 1,Token line offset = 67,Token in error = WHERE]" (I think its means about the first WHERE....But I don't sure..)
I try everything but I do'nt find a problem with that...
could somebody help me please?
Thanks in advance!!
ofir.
This doesn't look like T-SQL. Which RDBMS are you using?
--Jeff Moden
Change is inevitable... Change for the better is not.
October 7, 2012 at 1:57 pm
If you want to do it in one statement, you could try this....
INSERT INTO portfolioForExpert(ExpertID,portfolioID)
SELECT DISTINCT @0,'2'
FROM portfolioForExpert
WHERE NOT EXISTS(
SELECT 1
FROM portfolioForExpert
WHERE ExpertID=@0
AND portfolioID='2'
)
Or, simply wrap an IF...THEN around it like so....
IF NOT EXISTS (SELECT 1 FROM portfolioForExpert WHERE ExpertID=@0 AND portfolioID='2')
BEGIN
INSERT INTO portfolioForExpert(ExpertID,portfolioID)
VALUES (@0,'2')
END
October 9, 2012 at 10:26 am
If it is indeed SQL Server... I would think a long look at the MERGE INSERT/UPDATE command would be beneficial.
Very handy 😀
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply