Insert Not Exists Dual PK Problem

Mar 17, 2008

Hi  I am trying to populate a table with 2 FKs as its PK (SiteID and ProductDescID).  First 1) I add in all the products whose Manfacturer and Category are supposed to appear on the site and then 2) I add in all the extra products that are needed regardless of their manufacturer or category.   The problem I am having is if the product has already been added to the ProductCatTable due to its Manufacturer or Cateogry but is also in the SatForceProduct table.  The can’t insert duplicate PK error is thrown.  I don’t know how to do this IF NOT EXISTS statement (or what ever else may be needed) so that I can check whether a line from the Forced table needs to be added.  I am not passing in any parameters and I am expecting more than 1 line to be inserted in each of the statements. Please help -- 1) Populate      INSERT INTO            dbo.ProductCatTable            (SiteID, ProductDescID)             SELECT                dbo.SatSite.SiteID, dbo.ProductDesc.ProductDescID      FROM        dbo.SatManu INNER JOIN        dbo.ProductDesc ON dbo.SatManu.Manu = dbo.ProductDesc.Manu INNER JOIN        dbo.SatCats ON dbo.ProductDesc.Cat = dbo.SatCats.Cat INNER JOIN        dbo.SatSite ON dbo.SatManu.SatID = dbo.SatSite.SiteID AND         dbo.SatCats.SatID = dbo.SatSite.SiteID        2) Add Force Ins  IF NOT EXISTS(SELECT SiteID, ProductDescID FROM ProductCatTable WHERE ????????) BEGIN       INSERT INTO                        dbo. ProductCatTable                                    (SiteID, ProductDescID)       SELECT            SiteID, ProductDescID      FROM         dbo.SatForceProduct        END            Thanks in advance J

 

View 2 Replies


ADVERTISEMENT

Dual Xeon Or Dual P4 For SQL Server?

Jul 20, 2005

I am planning to build a server to be used as a SQL Server and web server.Right now I can only use a single box for both.I have read some threads were dual processors are having problems with someparallel queries and the suggestions of having sql server use a single CPU.My budget is limited so I am debating whether to get 2.6G dual xeon 533FSBor dual P4 800FSB (DRR@ ram) or stick with a speedy single cpu.If I get a dual cpu motherboard, is it a good idea to have 1 cpu used forsql server and the other for everything else?John Dalberg

View 5 Replies View Related

Insert If Not Exists

Mar 8, 2005

I have two tables that I have to compare:


Code:


Table:PR
WBS1 WBS2 WBS3
123-456 1000 01
123-456 1000 02
123-456 2000 02
567-890 2000 01
567-890 2000 02

Table:PR_Template
WBS2 WBS3
1000 00
1000 01
1000 02
2000 00
2000 01
2000 02

After Insert I should have:

wbs1 wbs2 wbs3
123-456 1000 00
123-456 1000 01
123-456 1000 02
123-456 2000 00
123-456 2000 01
123-456 2000 02
567-890 1000 00
567-890 1000 01
567-890 1000 02
567-890 2000 00
567-890 2000 01
567-890 2000 02





Basically, I need to insert the wbs2 and wbs3 where it does not exist in each wbs1.

What I have now will find the values that need to be inserted for a particular project but I don't know how to go through each project and perform the insert:

Select * from PR_template Where Not Exists
(Select Wbs1, Wbs2, Wbs3 from PR where PR.WBS2 = PR_Template.WBS2
And PR.WBS3 = PR_Template.Wbs3 and pr.wbs1 = '123-456')
Order by wbs2, wbs3 asc

Thank You

View 6 Replies View Related

Msg 156 On Insert Where Not Exists

Sep 28, 2007

I'm getting the following error on this query:

Insert into ILS_CustList values ('05-888', '05-888 - NV') where not exists (select jobid from ILS_CustList where jobid = '05-888')


Msg 156, Level 15, State 1, Line 1

Incorrect syntax near the keyword 'where'.


What's the problem?
Thanks!

View 5 Replies View Related

Insert Record If None Exists

Oct 6, 2003

Hello folks,

I am new to msSQL and ASP, I need some help writing an SQL statement that will first check to see if a combination or record exists, if none found thant it will add it. I am working a section of a site that adds favorites to the database. Each user can have more that one favorite hence it has to check for that unique combination of the fields, UserID and FavID

My Code:
Dim objConnection, objRecordset, strSQL
Dim strFavID, strUserID
strFavID = request.Form("favid")
strUserID = request.Form("userid")

Set objConnection = Server.CreateObject("ADODB.Connection")
Set objRecordset = Server.CreateObject("ADODB.Recordset")
objConnection.Open Application("ConnectionString")

strSQL = "INSERT INTO FAV (UserID,FavID) (SELECT DISTINCT " & strUserID & " AS UserID " & _
strFavID & " AS FavID" & " FROM FAV)" & _
" WHERE " & strUserID & " & " & strFavID & " NOT IN (SELECT UserID, FavID FROM FAV)"
response.Write(strSQL)
'response.End()

If strFavID <> "" Then
On Error Resume Next
objConnection.Execute(strSQL)

objConnection.Close
Set objConnection = Nothing
End If


This code is giving me a syntax error, how do I write the correct statement. I am using MS Access 2k

Thanks for your help

View 9 Replies View Related

Don't Insert If Record Exists

Jun 21, 2004

/*if key values exist don't insert new record*/
SELECT

/*if exists don't insert*/
CASE
WHEN ISNULL(gradeId, -1) = -1 THEN
INSERT INTO tblScores
(gtStudentId, assignmentId, score)
VALUES (@nStudent, @nAssignment, 0)
END

FROM tblScores
WHERE gtStudentId = @nStudent AND assignmentId = @nAssignment


tblScores has two fields comprising its primary key (gtStudentId, assignmentId) and the gradeId field is a required filed in this table.

I'm getting syntax errors when I click check syntax (near keywords insert from and end).

one other note: this CASE END is nested inside a BEGIN END loop, is this the problem? Is the 'End" of the 'Case' closing the 'End' of the 'Begin'?

thanks

View 6 Replies View Related

Insert If Not Exists Else Update

Apr 4, 2007

Hello,

it's me again :)
I've got a - what I think - simple question.
There is table A with Col1,Col2,Col3 and Table B with Col1,Col2,Col3

I want all rows from B in A. If a row already exist in A, then update all columns, else just insert the row.
Can someone please help me with a small syntax.
Thank you!

View 6 Replies View Related

Using NOT EXISTS In An INSERT Procedure

Jul 23, 2005

I am using the following code to insert records into a destination tablethat has a three column primary key i.e. (PupilID, TermID &SubjectGroup). The source table records all the pupils in a school with(amongst other things) a column (about 50) for each subject the pupilmight potentially sit. In these columns are recorded the study groupthat they belong to for those subjects. The destination table holds arecord per pupil per subject per term, against which the teacher willultimately record the pupils performance.The code as shown runs perfectly until the operator tries to insert aselection of records that include some that already exist. What I wouldlike it to do is, record those, which do not exist and discard theremainder. However, whenever a single duplicate occurs SQL rejects thewhole batch. I know that my solution will probably involve using the‘NOT EXISTS’ expression, but try as I might I cannot get it to work. Tofurther complicate things, the code is being run from within VBA usingthe RunSQL command.The variables ‘strFieldName’, ‘strGroup’ & ‘strTerm are declared at thestart of the procedure and originate from options selected on an Accessform.INSERT INTO dbo.yInterimReportData (PupilID, LastName, FirstName,TermID, SubjectGroup) SELECT PupilID, LastName, FirstName," & "'" &strTerm & "'" & "," & "'" & strGroup & "'" & "FROM dbo.Pupils WHERE (" &strFieldName & " = " & "'" & strGroup & "')Any Ideas?RegardsColin*** Sent via Developersdex http://www.developersdex.com ***

View 1 Replies View Related

IF Exists UPDATE ELSE INSERT Problem

Feb 18, 2004

Hi,

I have a 'Products' table (with: 'uid' and 'CatName' columns) and 'ProductCategory' table (with: 'uid', 'ProductID', 'CategoryID' columns).

I got stored procedure below to update or insert new row to 'ProductCategory' table whenever 'Products' table has been updated or new products has been added to it.

Update part works just fine but when new row has been added to 'Products' this storedProc dosn't insert it into 'ProductCategory' table, it does that only when 'ProductCategory' table is empty, I'm afraid it's because first column 'uid' in 'ProductCategory' table is an Identity column... I’m not sure how should I go about that problem. This is my stored procedure:

DECLARE @CatNo INT, @CatName varchar(10)
SET @CatNo = 2
SET @CatName = 'bracket'

IF exists (SELECT ProductID from ProductCategory, Products where ProductCategory.ProductID = Products.uid and Products.CatName = @CatName )
BEGIN
UPDATE ProductCategory SET CategoryID = @CatNo
FROM Products WHERE Products.CatName = @CatName and ProductCategory.ProductID = Products.uid
END
ELSE
BEGIN
INSERT INTO ProductCategory ( ProductID, CategoryID)
SELECT uid, @CatNo FROM Products
WHERE Products.CatName = @CatName
END

SET @CatNo = 3
SET @CatName = 'cable'

IF exists (SELECT ProductID from ProductCategory, Products where ProductCategory.ProductID = Products.uid and Products.CatName = @CatName )
BEGIN
UPDATE ProductCategory SET CategoryID = @CatNo
FROM Products WHERE Products.CatName = @CatName and ProductCategory.ProductID = Products.uid
END
ELSE
BEGIN
INSERT INTO ProductCategory ( ProductID, CategoryID)
SELECT uid, @CatNo FROM Products
WHERE Products.CatName = @CatName
END
(... Goes for another 37 categories)



Thank you for help.

Kooba

View 3 Replies View Related

RETURN Of IF NOT EXISTS INSERT Statement

Feb 20, 2006

Hi @ all

I'm using MSSQL and PHP.
I've got the following sql statement:

$msquery = IF NOT EXISTS (SELECT SerienNr FROM tbl_Auftrag a WHERE a.SerienNr='PC8') INSERT INTO tbl_Auftrag (BMS_AuftragsNr, SerienNr, AuftraggNr, Zieltermin, Kd_Name) VALUES ('455476567','PC8','1','2006-3-2','Fritz')

The Statement itself works fine, but i've got a problem getting a return value whether the insert has succeed, or not. :confused:
mssql_query() always returns true if there occured no error in the statement. But i need to know if the insert procedded or not.
I tried:

$result = mssql_query($msquery);
$succeed = mssql_rows_affected ($result);

And:

$result = mssql_query($msquery);
$succeed = mssql_num_rows($result);

to get the rows, affected by the statement, but both return:

supplied argument is not a valid MS SQL-Link resource

Anyone an idea?

View 2 Replies View Related

How To Update If Exists Else Insert In One SQL Statement

Jul 20, 2005

In MS Access I can do in one SQL statement a update if exists else ainsert.Assuming my source staging table is called - SOURCE and my targettable is called - DEST and both of them have the same structure asfollowsKeycolumns==========MaterialCustomerYearNonKeyColumns=============SalesIn Access I can do a update if the record exists else do a insert inone update SQL statement as follows:UPDATE DEST SET DEST.SALES = SOURCE.SALESfrom DEST RIGHT OUTER JOIN SOURCEON (DEST.MATERIAL = SOURCE.MATERIAL ANDDEST.CUSTOMER = SOURCE.CUSTOMER ANDDEST.YEAR = SOURCE.YEAR)This query will add a record in SOURCE into DEST if that record doesnot exist in DEST else it does a update. This query however does notwork on SQL 2000Am I missing something please share your views how I can do this inSQL 2000.ThanksKaren

View 6 Replies View Related

Record Exists Insert Or Update

Oct 25, 2007



I had implemented as in the link to insert or update
http://blogs.conchango.com/jamiethomson/archive/2006/09/12/SSIS_3A00_-Checking-if-a-row-exists-and-if-it-does_2C00_-has-it-changed.aspx


What i want to know is... how can i assume there are no duplicate records.
I used Distinct keyword and queried it showed me all are distint but some where i find some duplicates just don't know why i am having when i look at the data both are exactly same...

Please let me know how can i fix it.
Urgent..

Thanks

View 5 Replies View Related

Checking To See If A Record Exists Else Insert

Apr 30, 2008

I am checking to see if the source record is available in the target table using a lookup transformation and if not found i have to insert this record.
I have connected the error flow of the lookup transformation to the target. I am acheiving expected results, but is this the best practise? I have not connected to the green arrow to any task.

View 6 Replies View Related

Update Table If Record Exists Else Insert ?

Dec 17, 2007

Is there a way to structure a query to update an existing table record if it already exists, otherwise insert a new record into that table?

View 2 Replies View Related

Checking To See If A Record Exists And If So Update Else Insert

Feb 9, 2007

I've decided to post this as a sticky given the frequency this question is asked.

For those of you wishing to build a package that determines if a source row exists in the destination and if so update it else insert it, this link is for you.

http://blogs.conchango.com/jamiethomson/archive/2006/09/12/SSIS_3A00_-Checking-if-a-row-exists-and-if-it-does_2C00_-has-it-changed.aspx

Thanks Jamie!

If you want to do a similar concept to Jamie's blog post above, but with the Konesan's Checksum Transformation to quickly compare MANY fields, you can visit here:
http://www.ssistalk.com/2007/03/09/ssis-using-a-checksum-to-determine-if-a-row-has-changed/

Phil

View 60 Replies View Related

Loop,file Exists And Insert To Table...

May 20, 2008


Hi,

Here is the steps I should take...
1- Check for the log table and find run status ( there is a date field which tells the day run)
2- Lets say last day was 2008-05-15, So I have to check A1.DDDDMMYYY file exists in the folder for each
day like A1.20080516,A1.20080517 and A1.20060518 ( until today)
3- if A1.20080516 text file exist then I have to move it to the table and same thing for other dates
like if A1.20080517 exists I have to load it to table and so on

it looks like for each loop, first I have to get the last date and then I have to check the file exists for each date and
if the date file exists then I have to load it into table...

Please tell me How can I do it. it looks complex looping...

thanks,
J

View 4 Replies View Related

T-SQL (SS2K8) :: After Update Trigger - Only Insert Records Not Exists

Jul 30, 2014

I have an address table, and a log table will only record changes in it. So we wrote a after udpate trigger for it. In our case the trigger only need to record historical changes into the log table. so it only needs to be an after update trigger.The trigger works fine until a day we found out there are same addresses exist in the log table for the same student. so below is what I modified the trigger to. I tested, it seems working OK. Also would like to know do I need to use if not exists statement, or just use in the where not exists like what I did in the following code:

ALTER TRIGGER [dbo].[trg_stuPropertyAddressChangeLog] ON [dbo].[stuPropertyAddress]
FOR UPDATE
AS
DECLARE @rc AS INT ;

[code]....

View 2 Replies View Related

Checking Specific Columns Were NULL On Previous Insert (Using If EXISTS)

Aug 3, 2007

Thanks for your time:

Background: After Insert Trigger runs a sproc that inserts values into another table IF items on the form were
populated. THose all work great, but last scenario wont work: Creating a row insert based on Checking that all 22 other items from the prior insert (values of i.columns) were NULL:

IF EXISTS(select DISTINCT i.notes, i.M_Prior, i.M_Worksheet, ...
from inserted i
WHERE i.notes IS NOT NULL AND i.M_Prior = NULL AND i.M_Worksheet = NULL AND...)


BEGIN

Insert into dbo.Items2Fix ...

From inserted i

END

View 1 Replies View Related

Does Sql Server Have Something Like Dual

Dec 6, 2004

In oracle you can run queries against some built in virtual table called dual. Like below:

SELECT SEQ.NEXTVAL FROM DUAL

does sql server have anything similar?

View 2 Replies View Related

Dual Y-Axes

Dec 27, 2006

Greetings,

I'm wondering if it is possible to have two independent y-axes on a single chart?

I am wanting to plot data using a hybrid bar/line chart and have the values for the vertical bars show on the left y-axis and the values for the points on the line show on the right y-axis.

I've seen this sort of thing done on other platforms before, but don't know if it is possible with SQL Reporting. We are using SQL RS 2000 by the way.

Thanks
--
Anthony

View 6 Replies View Related

MS SQL Equivilent For Oracle DUAL

May 25, 2001

I am searching for the MS SQL equivilent for the Oracle Dual command.
select 'hello' from dual;

Jeff
jchie@vafb.com

View 2 Replies View Related

Dual Language Support

Sep 13, 2000

I've heard somewhere that SQL Server supports dual languages ? Can anyone confirm this ? How do you set it up please ?

Essentially we need to run our database in Japanese. It currently runs in english.

thanks
Paul

View 1 Replies View Related

Dual Processors & Performance

Mar 17, 1999

We have a 200MHz Pentium Pro based machine, with 128MB RAM running SQL Server 6.5. Because of performance issues, we are contemplating an upgrade to a dual 200MHz Pentium Pro processor with 256MB RAM. However, the vendor we are dealing with has suggested an upgrade to a single Pentiun II/333MHz first, and if this still causes problems, then to a dual P II/333MHz.
Does anyone have any suggestions from similar upgrades that they may have undergone?
We have 72MB allocated to SQL Server.

View 3 Replies View Related

Dual Core Processor

Aug 3, 2007

We are in process of moving to 64 bit HP servers with sql2005 standard edition. We were just wondering which is better option, to get a server with 2 dual core processor or to get a srver with just 4 processor? How does SQL2005 handle the hypertheading of dual processor?

Thanks,

Carlos

View 1 Replies View Related

Dual Redundant Environment

Mar 15, 2007

any tutorial on how to setup SQL Server in a dual redundant environment ?

thanks

View 3 Replies View Related

Deploying Dual Use Applications With SMO

Feb 20, 2008

I have created a C# Windows Forms application that can be run connected directly to SQL Server 2005 (publisher) for in-office users and to a SQL Express (subscriber) on a tablet PC for remote users. The server is set through configuration and the remote users sync using replication and it all works.

The issue I'm having is that I've found it necessary to install SQL Server management objects (SQLServer2005_XMO) on the clients who sit at desktops and never use replication or SMO. I believe this is because SMO has to be installed in the GAC and I can't just distribute the required dlls with my app.

Is there any way I can deploy this app to always connected users without installing SMO on their machines?

Thanks, Jamie Ide

View 1 Replies View Related

Creating A Table With A Dual Primary Key

Jul 12, 2004

This question may be a little complicated.

I am building a DTS Package that is moving data from our webstore (written in house) to a Warehouse Management System(WMS - Turnkey) and I've encountered a problem. both pieces of software have an orders table and an Ordered_Items table, related by the order_ID (makes sense so far). Here is the problem. The primary key on the webstore's Ordered_Items table is a single column (basically an Identity variable), while the primary key on the WMS's Ordered_Items table is a dual column primary key, between the Order_ID and the Order_LineID, so the data should be stored like:

OrderID Order_LineID
1 1
2 1
2 2
2 3
3 1
3 2
4 1

Get the Idea? So I have to create this new Order_LineID column. How can I accomplish this with a SQL statement?

Thanks!!!!!

View 5 Replies View Related

How Can I Use Dual Core CPU For Performance Improvement

Jul 17, 2007

Hi,



I am running MSSQL 2005 Standard edition on a two processor Intel Xeon 3GHz (dual-core) with 8GB RAM.



I notice in "Windows task manager CPU performance" while running a long SQL statement (takes 1.5 hours), only 1 logical (out of 4) is utilised at >70%. The remaining 3 logical processors hover around 10%



Using Performance monitor, the average read queue, write queue, and pages/sec also hovers around 25%, indicating no heavy physical disk/memory loading.



How can I set to utilise more physical/logical processor to improve the MSSQL performance ?



Thanks.

View 2 Replies View Related

DTSRUN Error On SQL7 With Dual Processor

Jul 31, 2001

Hi,

I am running SQL7 SP2 on a Compaq Proliant 5500 (NT4 Server SP6a) with dual processor.
When trying to schedule any jobs with DTSRUN I get the following error:

DTSRun.exe - Application Error
The instruction at "0x77f64d7b" referenced memory at "0x00000010". The memory could not be read.

Has anyone seen this error before ??

Regards,

Tim

View 1 Replies View Related

Single License Installed On Dual Processor

Oct 28, 2004

I currently have a single proc server in production and want to maintain a spare server that is dual-proc.

Is there a technical problem installing a single license SQL 2000 Standard on a dual-proc server? WIll it allow it or is it actually just a legal licensing requirement?

View 3 Replies View Related

Poor Performance On Dual Xeon Machine

Jul 23, 2005

Hi,I've been creating a db application using MS Access and MSDE. Only twoof us are using the application, and the server and the app both rungreat on my laptop (1.6 GHz Pentium M, 2GB RAM, W2KPro). Only problemis when I take my laptop home, my coworker loses access to the server.We recently purchased a dedicated server to run the db on at theoffice. It's a 2.8 GHz Dual Xeon, 2GB RAM, running XPPro. We alsobought SQL Server, but I installed the Personal Edition becuase we arenot using a server OS. It's my understanding that XP can utilize bothprocessors, and the Personal Edition can use both processors as well.(On a side note, why is Enterprise Manager showing that I have 4processors - why?) In addition, I understand PE has a work-loadgovernor that cripples performance when more than 5 TSQL commands arebeing run simultaneously.I backed up the db on my laptop and restored it on our new server. Butwhen I run the exact same queries with the exact same number of rows,my queries on the new server are take 3x longer(!?). Can someoneplease offer a few suggestions for why this is happening? What can Ido to improve performance on the server machine?Please let me know if I need to supply more information.Thanks,Alex

View 15 Replies View Related

Oracle Licensing For Dual Core Processors

Nov 23, 2005

Hi,Is there a reason why we have to pay more for licensing for a differentkind of processor?Why are we not charged for the Hyperthreading on some processors also.If Oracle is really conserned about the low end business market (smalland medium), then they should drop their attitude on Dual Coreprocessors.If they start charging as if it was a normal processor, and ask thenormal price, then they would get more of this market coming in.As long as Oracle keeps on having the attitude of charging more,because Intel or some other cpu vendor decided to mprove theirprocessors because of overheating problems, I will have the attitudethat I will keep on reoccomending alternatives for Orcle like Mysql /Postgre sql / Sybase, etc to the small/medium sector.Microsoft's pricing model on double core processors suddenly soundallot better.Oracle are shooting themselves in the foot! Or am I the only personfeeling this way?Shaun O'Reilly

View 2 Replies View Related

Transact SQL :: If Not Exists Some ID In One Table Then Insert ID And Description In Another Table

Jul 14, 2015

I need to find out if a Transaction ID exists in Table A that does not exist in Table B.  If that is the case, then I need to insert into Table B all of the Transaction IDs and Descriptions that are not already in. Seems to me this would involve If Not Exists and then an insert into Table B.  This would work easily if there were only one row.  What is the best way to handle it if there are a bunch of rows?  Should there be some type of looping?

View 12 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved