|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Tuesday, October 16, 2012 10:27 AM
Points: 1,
Visits: 8
|
|
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.
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Today @ 10:37 AM
Points: 32,893,
Visits: 26,769
|
|
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 "RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".
First step towards the paradigm shift of writing Set Based code: Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."
For better, quicker answers on T-SQL questions, click on the following... http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following... http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Monday, April 29, 2013 4:01 PM
Points: 7,
Visits: 275
|
|
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
|
|
|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Wednesday, May 08, 2013 10:24 AM
Points: 237,
Visits: 413
|
|
If it is indeed SQL Server... I would think a long look at the MERGE INSERT/UPDATE command would be beneficial.
Very handy
|
|
|
|