Showing posts with label explain. Show all posts
Showing posts with label explain. Show all posts

Tuesday, March 27, 2012

Duplicate records are being inserted with one insert command.

This is like the bug from hell. It is kind of hard to explain, so
please bear with me.

Background Info: SQL Server 7.0, on an NT box, Active Server pages
with Javascript, using ADO objects.

I'm inserting simple records into a table. But one insert command is
placing 2 or 3 records into the table. The 'extra' records, have the
same data as the previous insert incident, (except for the timestamp).

Here is an example. Follow the values of the 'Search String' field:

I inserted one record at a time, in the following order (And only one
insert per item):
airplane
jet
dog
cat
mouse
tiger

After this, I should have had 6 records in the table. But, I ended
up with 11!

Here is what was recorded in the database:

Vid DateTime Type ProductName SearchString NumResults
cgcgGeorgeWeb3 Fri Sep 26 09:48:26 PDT 2003 i null airplane 112
cgcgGeorgeWeb3 Fri Sep 26 09:49:37 PDT 2003 i null jet 52
cgcgGeorgeWeb3 Fri Sep 26 09:50:00 PDT 2003 i null dog 49
cgcgGeorgeWeb3 Fri Sep 26 09:50:00 PDT 2003 i null jet 52
cgcgGeorgeWeb3 Fri Sep 26 09:50:00 PDT 2003 i null jet 52
cgcgGeorgeWeb3 Fri Sep 26 09:50:22 PDT 2003 i null dog 49
cgcgGeorgeWeb3 Fri Sep 26 09:50:22 PDT 2003 i null cat 75
cgcgGeorgeWeb3 Fri Sep 26 09:52:53 PDT 2003 i null mouse 64
cgcgGeorgeWeb3 Fri Sep 26 09:53:06 PDT 2003 i null tiger 14
cgcgGeorgeWeb3 Fri Sep 26 09:53:06 PDT 2003 i null mouse 64
cgcgGeorgeWeb3 Fri Sep 26 09:53:06 PDT 2003 i null mouse 64

Look at the timestamps, and notice which ones are the same.

I did one insert for 'dog' , but notice how 2 'jet' records were
inserted
at the same time. Then, when I inserted the 'cat' record, another
'dog' record was inserted. I waited awhile, and inserted mouse, and
only the mouse was inserted. But soon after, I inserted 'tiger', and 2
more mouse records were inserted.

If I wait awhile between inserts, then no extra records are inserted.
( Notice 'airplane', and the first 'mouse' entries. ) But if I insert
records right after one another, then the second record insertion also
inserts a record with data from the 1st insertion.

Here is the complete function, in Javascript (The main code of
interest
may start at the Query = "INSERT ... statement):
----------------------
//Write SearchTrack Record -----------

Search.prototype.writeSearchTrackRec = function(){
Response.Write ("<br>Calling function writeSearchTrack \n"); // for
debug
var Query;
var vid;
var type = "i"; // Type is image
var Q = "', '";
var datetime = "GETDATE()";
//Get the Vid
// First - try to get from the outVid var of Cookieinc
try{
vid = outVid;
}catch(e){
vid = Request.Cookies("CGIVid"); // Gets cookie id value
vid = ""+vid;
if (vid == 'undefined' || vid == ""){
vid = "ImageSearchNoVid";
}
}

try{
Query = "INSERT SearchTrack (Vid, Type, SearchString, DateTime,
NumResults) ";
Query += "VALUES ('"+vid+Q+type+Q+this.searchString+"',
"+datetime+","+this.numResults+ ")";
this.cmd.CommandText = Query;
this.cmd.Execute();
}catch(e){
writeGenericErrLog("Insert SearchTrack failed", "Vid: "+vid+"
- SearchString:: "+this.searchString+" - NumResults: "+this.numResults
, e.description);

}
}//end

--------------------
I also wrote a non-object oriented function, and created the command
object inside the function. But I had the same results.

I know that the function is not getting called multiple times
because I print out a message each time it is called.

This really stumps me. I'll really appreciate any help you can
offer.

Thanks,

George"george" <georgem@.crystalgraphics.com> wrote in message
news:620c2f02.0309261014.20789ca0@.posting.google.c om...
> This is like the bug from hell. It is kind of hard to explain, so
> please bear with me.

<snip
I have no idea what's causing the issue, but the usual advice is to use
Profiler to trace the SQL which is getting sent to the database. You may see
something in the statements which helps you pin down what's going on.
Another thing to check is if there is an INSERT trigger on the table.

Simon|||"Simon Hayes" <sql@.hayes.ch> wrote in message news:<3f756b8c_4@.news.bluewin.ch>...
> "george" <georgem@.crystalgraphics.com> wrote in message
> news:620c2f02.0309261014.20789ca0@.posting.google.c om...
> > This is like the bug from hell. It is kind of hard to explain, so
> > please bear with me.
> <snip>
> I have no idea what's causing the issue, but the usual advice is to use
> Profiler to trace the SQL which is getting sent to the database. You may see
> something in the statements which helps you pin down what's going on.
> Another thing to check is if there is an INSERT trigger on the table.
> Simon

Thanks for your help. I found the cause. This is a web page that
contained a form. The form had a button with an onClick event handler
that evolked a script that submitted the form. Well, I added another
form to the page, and wanted the form to be submitted when the user
pushed the 'enter' key, so I focused the new submit button, and put an
onSubmit event handler in the new form. But the onSubmit event handler
evolked the before-mentioned script, so apparently two forms were
being submitted. One form had a hidden form element that contained
the previous 'search string' and the other form had an input box for
the current 'search string'. It explains why 2 db inserts happened
when I submitted the form, but it does not explain why sometimes 3
inserts happened. Anyway, it was kind of difficult to debug, because
it was not evident that 2 forms were being submitted, because, to the
user, the page worked just fine.

Well, perhaps this incident will help someone else down the road who
may run into this situation.

George

Sunday, March 11, 2012

Dublicates

can someone please explain to me how to append data to current database tables?

If I have infromation from access and want to had the (NEW) information to current SQL tables how to I append without writing over current table information and without creating dups if the infromation currently exists within the table?

I would like to keep the current table information and append anything new only.

ThanksA dup key in a sproc will raise by it's self to the calling sproc..

But heres an example

USE Northwind
GO

CREATE TABLE myTable99(Col1 int PRIMARY KEY, Col2 char(1))
GO

DECLARE @.Error int, @.Col1 int, @.Col2 char(1)

SELECT @.Col1 = 1, @.Col2 = 'A'

INSERT INTO myTable99(Col1,Col2) SELECT @.Col1, @.Col2

SELECT @.error = @.@.ERROR

SELECT 'Error code: ' + CONVERT(varchar(5),@.Error)

SELECT @.Col2 = 'B'

INSERT INTO myTable99(Col1,Col2) SELECT @.Col1, @.Col2

SELECT @.error = @.@.ERROR

IF @.Error <> 0
BEGIN
UPDATE myTable99 SET Col2 = @.Col2 WHERE Col1 = @.Col1
SELECT @.Error = @.@.ERROR
END

SELECT 'Error code: ' + CONVERT(varchar(5),@.Error)

SELECT * FROM myTable99
GO

DROP TABLE myTable99
GO

Wednesday, March 7, 2012

DTSX package data transfer error

I will try to explain things the best I can.

When the data is transferred from source to destination (replace not append), the data in one field in one table is incorrect. Both source and destination tables have the same number of rows (8493). The ProductID field data range at the source is from 58958 to 73008. When the table is copied the ProductID field data runs from 1 to 8493.

What would cause this skewing of data?

This happens on brand new dtsx packages and this only happens in one field out of 5 different tables.

I am baffled. Any help is appreciated.

Thanks.

Andrew

Sounds like ProductID is an identity field.

In the OLE DB destination, ensure that you are using a data access mode of "Table or view - fast load" and then make sure the check box is selected for "Keep identity."

See if that helps.|||

Thanks for the quick response Phil.

Where exactly do I look for this setting in Visual Studio?

I must be overlooking it.

Thanks.

Andrew

|||It's in the OLE DB Destination component. Double click on it.|||Did you develop the package, or use the Import/Export wizard?|||I devolped it.|||

FordyH500HP wrote:

I devolped it.

Then what destination are you using? Use the OLE DB Destination if you're not using it already.|||

I'm using the Transfer SQL Server Objects Task from the toolbox.

Will that work for what you are talking about?

|||

FordyH500HP wrote:

I'm using the Transfer SQL Server Objects Task from the toolbar.

Will that work for what you are talking about?

Ahh... What values for the options under "Table Options" do you have?|||

Sorry if I should have stated that sooner.

I'm just above Newbie status in Visual Studio.

CopyIndexes - True

CopyTriggers - False

CopyFullTextIndexes - False

CopyPrimaryKeys - False

CopyForeignKeys - False

GenerateScriptsInUnicode - False

|||What happens if you set CopyPrimaryKeys and CopyForeignKeys to True?|||Same result. It renumbers the ProductID field starting with 1.|||

FordyH500HP wrote:

Same result. It renumbers the ProductID field starting with 1.

Yep, doing a quick search shows that the Transfer SQL Server Objects task does not support identity columns. Will future versions?

Let's find out:
[Microsoft follow-up]

You can do this on your own though, by using an execute sql task in the control flow to truncate the destination tables and then as many data flows as you have tables. Inside the data flows, you can use an OLE DB source and hook it up to an OLE DB destination, and use the options I suggested above.|||

Ok. I think I understand.

So I want to check Keep Identity then in the data flow destination?

Also, can I put multiple Source/Destinations in one data flow?

|||Yep

Friday, February 24, 2012

DTS: Inserting the contents of a global variable into a table via a loop

Hello,

I am wondering if anyone can help me. I am fairly new to DTS and am having some difficulty getting something to work. I will explain below:

I have one table (table_a) which contains a listing of contact details. I want to copy the data from table_a to table_b. The tricky part is that I want to insert certain items into table_b depending on a conditional statement on the items in table_a. (ie. If value in table_a.column_a is 1 then insert "good" into table_b.column_a, etc)

I have done a lot of looking and found that an ActiveX Script Task is the answer. So I now have an Execute SQL Task that selects all the data from table_a into a global variable recordset, an ActiveX Script Task that loops through the recordset, determines the necessary values and modifies the SQL Statement of a temporary Execute SQL Task with the required INSERT statment (as explained in this article: http://www.databasejournal.com/features/mssql/article.php/1461581).

I run it and it all runs fine except that no data is inserted into the table!!! The SQL Statement in the temp Task does get modified with the INSERT statement and inserts the data fine when forced to run. But it appears that the temp Task is not being executed within the ActiveX Script.

Can anybody tell me how to force a step to run in this loop-based scenario, or if there is a much better way I could do this.

Thanks for any help. LeeIf I understand what you are trying to do...
Inputvalue = 10

if Inputvalue < 10, Output Value = 20, else Output Value = 30.

If you are trying to make those types of changes, then you can do some simple Transforms.

With two data sources, you can add a Transform Data Task.

You can modify (delete, then add) an activex transform within the Transform task that will allow you to modify outgoing data based on the data coming in.

If you need more help on this, see books online, ActiveX Script, then choose DTS.|||Thanks, yes. I was just looking into that as I received your post and it does indeed look like the ActiveX Transform option is the way to go.

I'm impressed you could decipher what I was after!

Thanks for the help.