Showing posts with label tables. Show all posts
Showing posts with label tables. Show all posts

Friday, March 30, 2012

How to insert records into two tables that references each other?

I have two tables , A1 and B1 . B1 has a reference key pointing to A1.ID which is a primary key. Now I want to INSERT a record into both table.

CREATE TABLE a1 (
T_ID INTEGER IDENTITY(1,1) PRIMARY KEY,
T_Test INTEGER NOT NULL,
);

CREATE TABLE B1 (
B_ID INTEGER IDENTITY(1,1) PRIMARY KEY,
T_SID INTEGER NOT NULL REFERENCES a1(T_ID),
);

-- A function to get the next primary key value

-- Get the next unique key from A1
CREATE FUNCTION [dbo].[getTicketNo]
(
)
RETURNS INT
AS
BEGIN
RETURN (SELECT Max(T_ID) from A1)
END

I can only insert a record into a table at a time. and I rely on dbo.getTicketNo() to get the lastest T_ID

I wonder how do you insert a new record into A1 and B1 and maintain referential integrity? You must call Insert twice to accomplish the job right?

What I want to insert a record into B1.SID is the primary key value (T_ID) that was created during "Insert into A1 values (0)", using dbo.getTicketNo()
I worry if I am going to insert a record into A1 first, then insert a record into B1 next, there is a chance the data wont be consistent; as I illustrated a situation below:

t A1 B1
0 (0)Insert
1 (1)Insert
2 (0)Insert
3 (1)Insert

dbo.getTicketNo() at t3 may return the wrong value if another process (1)insert is executed, that will change the primary key number to 2 which is incorrect

Can I lock up A1 and then unlock A1 after I finished working with B1?

If so, what would happen to the application if it tries to access into A1 during it is locked?

If you ask me this not a good approach to get the identity value.
You can use the following objects to get the last inserted identity value.

@.@.Identity or Scope_Identity()

1.Insert the data on Master table (A1)
2.Then get the last inserted value from those system functions or global varibale
3.Use the values on Child table (B1)

Example,

Insert into A1(T_Test) values (@.T_Test);
Insert into B1(T_SID) values (@.@.Identity)
--or
Insert into B1(T_SID) values (Scope_Identity())

NOTE: Don't use MAX function to get the last inserted identity value. It may be
Inserted by other concurrent users.

|||


Thank you ManiD

From your code, is there any chance that another INSERT query is executed by other concurrent users after the 1st Insert and before the 2nd Insert in your case?

time 0:00:00 (User 0) Insert into A1(T_Test) values (@.T_Test);
time 0:00:20 (User 1) Insert into A1(T_Test) values (@.T_Test);
time 0:00:21 (User 0) Insert into B1(T_SID) values (@.@.Identity)

If it is possible, what is the best approach to ensure that the second Insert into B1 is consistent to the master A1?

Thanks for your precious time

|||

In this case,

@.@.Identity hold the User1 Identity data..

So it always good idea to use SCOPE_IDENTITY() instead of using @.@.Identity..

SCOPE_IDENTITY always hold the current user's identity data...

|||

Ok,,, thank you ManiD

Let me do some research on SCOPE_Indentity() ...

Thank you for your kind replies

|||

When I google it, found some people mispelled SCOPE_IDENTITY() to SCOPE_INDENTITY() .....

Here is the MSDN for SCOPE_IDENTITY() if anyone got the similar issues can have a look

http://msdn2.microsoft.com/en-us/library/ms190315.aspx

How to insert records into related tables?

Hi,
I have 3 tables:
Employees with the fields:idEmployee and employeeName
Roles with the fields:idRole and roleName.
An employee can have one or many roles.
I created the table EmployeeRoles with the fields: id,idEmployee,idRole.
idEmployee and idRole are foreign keys.
I want to insert a new employee into Employees table, but I have to insert idEmployee field into EmployeeRoles table.

How can I do this?
Thanks

This should be handled by your application. If you are using identity fields you can use the SCOPE_IDENTITY function to return the last value for the identity column.

WesleyB

Visit my SQL Server weblog @. http://dis4ea.blogspot.com

|||Hi,
Can you please help me with the sintax?
Thanks|||To create a table, you can do something like:

CREATE TABLE Employee

(

EmployeeID INT NOT NULL

,EmployeeFirstName VARCHAR(15) NOT NULL

,EmployeeLastName VARCHAR(15) NOT NULL
)


CREATE TABLE Role

(

RoleID INT NOT NULL

,RoleName VARCHAR(15) NOT NULL

)


CREATE TABLE EmployeeRole

(

RoleID INT NOT NULL

,EmployeeID INT NOT NULL

)

INSERT Employee

(

EmployeeID

,EmployeeFirstName

,EmployeeLastName

)

VALUES

(

1

,'John'

,'Wayne'

)

INSERT Role

(

RoleID

,RoleName

)

VALUES

(

1

,'Rooster Cogburn'

)


INSERT EmployeeRole

(

RoleID

,EmployeeID

)

VALUES

(

1

,1

)




As WesleyB mentioned, you can take this a step further and make the ID columns INDENTITY columns and use the SCOPE_INDENTITY function to retrieve the value for the most recently inserted row. You can also add some declarative referential integrity if you wish. Check out Books Online for more information on this.

BTW - I'm writing this without access to a SQL Server to test the scripts so please forgive any typos.

HTH...

Joe

|||Ok,
Thanks.
I used scope_identity and it worked.|||

I addition you could have used OUTPUT to redirect the output to a resultset with the assigned IDENTITIES per row. This feature is SQL Server 2005 and above only.

Jens K. Suessmeyer

http://www.sqlserver2005.de

How to insert records into related tables?

Hi,
I have 3 tables:
Employees with the fields:idEmployee and employeeName
Roles with the fields:idRole and roleName.
An employee can have one or many roles.
I created the table EmployeeRoles with the fields: id,idEmployee,idRole.
idEmployee and idRole are foreign keys.
I want to insert a new employee into Employees table, but I have to insert idEmployee field into EmployeeRoles table.

How can I do this?
Thanks

This should be handled by your application. If you are using identity fields you can use the SCOPE_IDENTITY function to return the last value for the identity column.

WesleyB

Visit my SQL Server weblog @. http://dis4ea.blogspot.com

|||Hi,
Can you please help me with the sintax?
Thanks|||To create a table, you can do something like:

CREATE TABLE Employee

(

EmployeeID INT NOT NULL

,EmployeeFirstName VARCHAR(15) NOT NULL

,EmployeeLastName VARCHAR(15) NOT NULL
)


CREATE TABLE Role

(

RoleID INT NOT NULL

,RoleName VARCHAR(15) NOT NULL

)


CREATE TABLE EmployeeRole

(

RoleID INT NOT NULL

,EmployeeID INT NOT NULL

)

INSERT Employee

(

EmployeeID

,EmployeeFirstName

,EmployeeLastName

)

VALUES

(

1

,'John'

,'Wayne'

)

INSERT Role

(

RoleID

,RoleName

)

VALUES

(

1

,'Rooster Cogburn'

)


INSERT EmployeeRole

(

RoleID

,EmployeeID

)

VALUES

(

1

,1

)




As WesleyB mentioned, you can take this a step further and make the ID columns INDENTITY columns and use the SCOPE_INDENTITY function to retrieve the value for the most recently inserted row. You can also add some declarative referential integrity if you wish. Check out Books Online for more information on this.

BTW - I'm writing this without access to a SQL Server to test the scripts so please forgive any typos.

HTH...

Joe

|||Ok,
Thanks.
I used scope_identity and it worked.|||

I addition you could have used OUTPUT to redirect the output to a resultset with the assigned IDENTITIES per row. This feature is SQL Server 2005 and above only.

Jens K. Suessmeyer

http://www.sqlserver2005.de

sql

How to insert primary keys without using identity

I have the following issue
- my database consists of tables with one ID field as primary key.
for each INSERT the 'next' value from this ID field is extracted
from a table called TableList.
- this works perfectly fine, as long as I insert one record at a time:
but now I would like to run a command such as
INSERT INTO dest (name)
SELECT name
FROM src
i.e. without being able to specify the ID value.
Has anybody implemented this
(i would prefer not to use identity columns or use cursors),
possible with triggers?

thanks for your time,

AndreIt is possible with cursors, but would be awkward and I expect subject to problems if the table was having lots of records added by lots of users at the same time...

Why do you not want to use Autoincrement?

Wednesday, March 28, 2012

how to insert image

Hi there
I have a Stored procedure which stores image in SQL tables , if u are interested let me know , I will send you the code
Regards
RajeshHi Rajesh,

I am presently working on storing and retrieving image on SQL 2000 using VB6.

I would be very much grateful if you could pls send me codes u have that can hlp me.

Thanks a lot

prakash

NB my email is prak_07@.servihoo.com|||Originally posted by rajeshm
Hi there

I have a Stored procedure which stores image in SQL tables , if u are interested let me know , I will send you the code

Regards

Rajesh

Hi There,

I would also be interested to have this stored procedure.

I would be greatful if you send it to me
Thanks

gunnar.westerberg@.iftech.se|||Rajesh I would also like to have the code and the SP for using the same in my project. please send me a copy also. My mail Id is dineshyadav@.yahoo.com. Thanks in Advance
Regards
Dinesh|||Can you post the Code ?

BTW what are you store in the DB ? the path to the Image ?

Thanks,
Eyal|||Hi Rajesh,
Does your SP supports storing upto 100 MB of data??
Please send me the code ...It would be of great help for me.
I am waiting for a long time for such help. I have my own one ...but it fails for 100 MB. Please send urs.

My E-mail id is avneesh@.newgen.co.in

Thanks in advance and hope u send it ASAP.

Avneesh|||Originally posted by rajeshm
Hi there

I have a Stored procedure which stores image in SQL tables , if u are interested let me know , I will send you the code

Regards

Rajesh

Hai ...
Please send me your code too. Cause i need it for my next project.
thx

ps : My email desy_g@.hotmail.com|||hello rajesh
I want to store word documents and pictures in SQL server 2000.is it possible with SQL server2000.if please send me your code in
subyphilip@.indiatimes.com|||I have seen similar thread in other forums with no results for getting SP from the originator.

You can use REDADTEXT/WRITETEXT/UPDATETEXT to dealwith text/image datatypes and refer to books online for more information.

Also may check under Umachander's (http://www.umachandar.com/) homepage for the code.|||Please send me the code

My Email-id is mdabidullah@.indiatimes.com

Thanks in advance and hope u send it ASAP.

Abid

Originally posted by rajeshm
Hi there

I have a Stored procedure which stores image in SQL tables , if u are interested let me know , I will send you the code

Regards

Rajesh|||This looks like a hot thread. I also have a similar problem - inserting into image columns from a script. Im interested in that code too. Cheers!|||I bet you will not get reply/mail from the originator about the code.
As referred you browse thru the Umachander's page and also search under Planet source code (http://www.planet-source-code.com) for similar issue, this will save your time and gives you more resources.|||Thanks for that link. Once i come up with the soln to ma kinda of problem, i will share it with you forum members.|||Appreciate your spirit.|||Originally posted by rajeshm
Hi there

I have a Stored procedure which stores image in SQL tables , if u are interested let me know , I will send you the code

Regards

Rajesh

I too would be interested in the code. Thanks
victor@.keilman.com|||what's up with this "know-how"?? hasn't anybody tried it at all?

create table tblImages (ImageID int identity(1,1) not null, Contents image null)
go
create procedure sp_AddImage (
@.image image )
as
declare @.ImageID int, @.txtptr varbinary(16)
begin tran
insert tblImages (Contents) values (null)
set @.ImageID = scope_identity()
--this part is to ensure that NULL is written to the image field
update tblImages set Contents = null where ImageID = @.ImageID
select @.txtptr = textptr(Contents)
from tblImages
where ImageID = @.ImageID
writetext tblImages.Contents @.txtptr @.image
if @.@.error != 0 begin
raiserror ('Failed to write new image to the table!', 15, 1)
rollback tran
return (1)
end
commit tran
select RetVal = @.ImageID
return (0)
go
exec sp_AddImage 0x00000fffff|||ms_sql_dba, just think of all the people you have made happy today!

A thousand smilies for you: POWER(POWER( :) :) :) :) :) :) :) :) :) :) , 10), 10)|||ms_sql_dba: I like the code. I have never worked with image/text data before, so I don't know how long it would have taken me to get that insert null trick to recover the identity value. Nice job.|||Please send the code to me

lord_n@.msn.com|||Hi,
I have never worked with images in SQL. Your code works like butter. Beautiful. But I have a question. How would I use it? What would an application pass to me(image, image reference or path to the image?)? How would I give the image back to the application?
Thanks.|||i am interested in this script.
i am happy if you send to me...thanks
my e-mail is hckot@.yahoo.com.hk|||hi,

I am Manoj from INDIA.

I have installed Microsoft SQL SERVER 2000 Desktop Edition.

I want to see all the databases created in SQL SERVER in the VISUAL BASIC. If it were ORACLE we can query by "select * from tab" from VB and populate the listbox with the resulset. But in SQLSERVER the query is not working. Ehat should i do?

Awaiting for ur reply..

-Manoj|||Do you need all the databases or all the tables

select * from tab in Oracle would give you all the tables .. not the databases.

For selecting all tables from a particular database in sql server

select * from sysobjects where type = 'U'|||Originally posted by rajeshm
Hi there

I have a Stored procedure which stores image in SQL tables , if u are interested let me know , I will send you the code

Regards

Rajesh

Rajesh!
I would like to look at your stored procedure. Can you send it to me I would be very greatful.

papd_sweden@.hotmail.com

Thanks in advance
//Moon|||Dosen't anyody read the entire post ....

Originally posted by ms_sql_dba
what's up with this "know-how"?? hasn't anybody tried it at all?

code:------------------------
create table tblImages (ImageID int identity(1,1) not null, Contents image null)
go
create procedure sp_AddImage (
@.image image )
as
declare @.ImageID int, @.txtptr varbinary(16)
begin tran
insert tblImages (Contents) values (null)
set @.ImageID = scope_identity()
--this part is to ensure that NULL is written to the image field
update tblImages set Contents = null where ImageID = @.ImageID
select @.txtptr = textptr(Contents)
from tblImages
where ImageID = @.ImageID
writetext tblImages.Contents @.txtptr @.image
if @.@.error != 0 begin
raiserror ('Failed to write new image to the table!', 15, 1)
rollback tran
return (1)
end
commit tran
select RetVal = @.ImageID
return (0)
go
exec sp_AddImage 0x00000fffff
------------------------|||Hello, people! This thread is 2 YEARS OLD! And the guy never posted again...

Everything on this thread should be deleted except MS_SQL_DBA's code.|||Where is the moderator when you need him the most :)|||can you send me your proc that store images to ..

does your code do this..

get all rows in a tables.. in a stroc proc..and save them back to a new table and one table is a image field.. ?

beaulieu1@.hotmail.com

thanx... been searching for tree days now..on this

Originally posted by rajeshm
Hi there

I have a Stored procedure which stores image in SQL tables , if u are interested let me know , I will send you the code

Regards

Rajesh|||Wow! This thread still hasn't been read properly. ;)|||Do you think some ppl are just dumb? or someone is using diff logins just for the fun of it and posting the same request over and over again......|||Hey Patrick ... what does SQL Padawan mean|||Originally posted by Enigma
Hey Patrick ... what does SQL Padawan mean

Dear SQL Apostle,

Have you seen Star Wars? Before you become a Jedi, u'r a Padawan...
:)

This thread is really going out from it original topic....|||Hmm .. Star Wars was never my kinda science fiction ... unbelievable (pun intended) ... Gattaca is more like it ...

So ... when you get your fourth star ..do you become a SQL Jedi Knight ;)

BTW .. this thread was never on topic anyway.|||Kinda already taken...

http://www.sqlteam.com/forums/members.asp|||Gattaca gets four stars from me.

"I never saved anything for the swim back."|||Originally posted by blindman
Gattaca gets four stars from me.

"I never saved anything for the swim back."

My favourite quote too ...|||is this code still available|||Dawn Of The Dead Post|||The code is available halfway through this thread. I believe ms_sql_dba was good enough to put it in here for everyone.

So why can't this thread get sent to the marketplace?|||Send it to the trash bin.|||soz of the dumb question before...didnt actually read the whole thread SOZ...listen what exactly does this bit of code do?

PLEASE HELP i really need to store images on my database....

THANKS|||It's best not to store images in the database, rather, store only a
reference to the image in the database, ie, a text field
containing "c:\wwwroot\images\userphotos\user1\user1.jpg"

If you are having people upload files to your site, you can use asp's
filesystemobject to dynamically create such a file structure.

Follow this http://www.sqlteam.com/item.asp?ItemID=986 link for more information to store images to database.|||I have a question: How can I copy the binary value from tableA.columnA to tableB.columnB? Both of them are image fields. I have tried using updatetext but kept giving the error 'NULL textptr (text, ntext, or image pointer) passed to UpdateText function'. Does you have any samples or code snippets? Thanks!

-------
Originally posted by Satya
I have seen similar thread in other forums with no results for getting SP from the originator.

You can use REDADTEXT/WRITETEXT/UPDATETEXT to dealwith text/image datatypes and refer to books online for more information.

Also may check under Umachander's (http://www.umachandar.com/) homepage for the code.|||Originally posted by Patrick Chua
Do you think some ppl are just dumb? or someone is using diff logins just for the fun of it and posting the same request over and over again...... good questions, hey?!|||Originally posted by stephanie_lim
I have a question: How can I copy the binary value from tableA.columnA to tableB.columnB? Both of them are image fields. I have tried using updatetext but kept giving the error 'NULL textptr (text, ntext, or image pointer) passed to UpdateText function'. Does you have any samples or code snippets? Thanks!

------- you have to update your text/image field to null before retrieving the txtptr.|||why not using 3rd tool to help u.
I suggest u trying borland datapump(an extended tool in delphi install package).
Last ,don't forget u can write a program(vb,delphi,java,etc) to do it by yourself .
It's very easy.|||Originally posted by Satya
It's best not to store images in the database, rather, store only a
reference to the image in the database, ie, a text field
containing "c:\wwwroot\images\userphotos\user1\user1.jpg"

If you are having people upload files to your site, you can use asp's
filesystemobject to dynamically create such a file structure.

Follow this http://www.sqlteam.com/item.asp?ItemID=986 link for more information to store images to database. satya,

you sound like it's been an industry standard to store the actual images as file system objects, rather than in the database. it's actually a matter of trust you have with one over the other. it also depends on the app itself, - surely you don't plan to search for the contents of the file even if it is of type text if it is stored in the database. i successfully implemented storing hard-copy documents in the database that get displayed whenever a hard-copy is requested by the user. i also stored report definitions as well as report outputs into image fields so that they can be viewed using the same format/the same content as when they originally produced. i just don't see a justification for this dogmatic approach. do you?|||ON the basis of performance and managebility I recommended (industry standard as you mentioned) to use the file paths instead of storing them on the database.

If your approach are fetching good results without any issues... say no issues with the database consistency then follow it. As far as my experience is concerned I tend to suggest to use file paths rather than storing images directly on the database.:)|||could you be so kind to refer me to a white paper or a publication where this "industry standard" is being discussed? i'd like to at least conceptually imagine the reasoning for this "standard" to even exist.

thanks|||[http://www.sql-server-performance.com/q&a17.asp] for instance.|||even though it's not a white paper or publication, but rather a personal opinion of an author, i'd like to comment on it:

"...storing BLOB objects in SQL Server is cumbersome..." - only if you are not sure how to do it or the method to do it is not the most efficient

"The most commonly accepted way of managing images for a website is not to store the images in the database, but only to store the URL in the database, and to store the images on a file share that has been made a virtual folder of your web server." - please note that (while still being the personal opinion of the author) the paragraph above states that it's "the most commonly accepted". i'd question that because the shops that i worked for and know of accepted the "more cumbursome" way of dealing with images and documents, and that's how they managed to be more successful than the others. you'd ask me what happened to the others? one example is when system administrator decided to reorganize the file structure which resulted in web-site being out of commission until the "more cumbursome" way was introduced and resolved the issue...forever ;)|||I need the code. Thank you.
My email: kim78@.streamyx.com|||Is this thread ever gonna die down ?|||The Code:

Dim ReferTread as this.tread
Dim Understand boolean

Answer=ReferTread.prevpage
if Understand=1
Response.write("You figured it out!")
elseif Understand=0
Response.write("Forget programming")
end if|||OK, ms_sql_dba's code was neat, but there is ADO.Stream object that pretty much does the same.

The alternative to it is the following (please do not ask to repost!!!) which I left unmodified:

-- Ensure QUOTED_IDENTIFIER setting is OFF
set quoted_identifier off
go
-- Drop all participating objects
if object_id('dbo.sp_OA_CreateFile') is not null
drop procedure dbo.sp_OA_CreateFile
go
if object_id('dbo.sp_OA_WriteLineToFile') is not null
drop procedure dbo.sp_OA_WriteLineToFile
go
if object_id('dbo.sp_OA_CloseFile') is not null
drop procedure dbo.sp_OA_CloseFile
go
if object_id('dbo.sp_StoreImage2Table') is not null
drop procedure dbo.sp_StoreImage2Table
go
if object_id('dbo.tblDocumentSubCategories') is not null
drop table dbo.tblDocumentSubCategories
go
if object_id('dbo.tblDocumentCategories') is not null
drop table dbo.tblDocumentCategories
go
if object_id('dbo.tblDocumentTypes') is not null
drop table dbo.tblDocumentTypes
go
if object_id('dbo.tblImages_tmp') is not null
drop table dbo.tblImages_tmp
go
if object_id('dbo.fn_DateFromCharacter') is not null
drop function dbo.fn_DateFromCharacter
go
if object_id('dbo.tblDocuments') is not null
drop table dbo.tblDocuments
go
-- Build the database structure to store binary data
create table dbo.tblDocumentTypes (
TypeID int identity(1,1) not null primary key clustered,
[Description] nvarchar(450) not null )
go
create table dbo.tblDocumentCategories (
CategoryID int identity(1,1) not null primary key clustered,
[Description] nvarchar(450) not null )
go
create table dbo.tblDocumentSubCategories (
SubCategoryID int identity(1,1) not null primary key clustered,
CategoryID int not null,
[Description] nvarchar(450) not null )
go
alter table dbo.tblDocumentSubCategories
add constraint FK_DocumentCategories foreign key (CategoryID)
references dbo.tblDocumentCategories (CategoryID)
go
alter table dbo.tblDocumentCategories
add constraint UC_UniqueDocumentCategory unique ([Description])
go
create table dbo.tblImages_tmp (fImage image null)
go
create table dbo.tblDocuments (
DocumentID int identity(1,1) not null primary key nonclustered,
TypeID int not null,
CategoryID int not null,
SubCategoryID int not null,
DocumentName nvarchar(2000) not null,
[Description] nvarchar(255) not null,
DocumentSize int not null,
AlternateName nchar(12) not null,
CreattionDate datetime not null,
LastModified datetime not null,
LastAccessed datetime not null,
Attributes varchar(10) not null,
DocumentImage image null)
go
create function dbo.fn_DateFromCharacter (
@.DatePart char(8),
@.TimePart char(6) ) returns datetime
as begin
declare @.Date datetime, @.MonthDay char(4), @.Year char(4), @.Month char(2), @.Day char(2)
declare @.Hour char(2), @.Minute char(2), @.Second char(2)
set @.MonthDay = reverse(cast(reverse(@.DatePart) as char(4)))
set @.Month = cast(@.MonthDay as char(2))
set @.Day = reverse(cast(reverse(@.MonthDay) as char(2)))
set @.Year = cast(@.DatePart as char(4))
set @.TimePart = right('000000'+@.TimePart, 6)
set @.Hour = cast(@.TimePart as char(2))
set @.Second = reverse(cast(reverse(@.TimePart) as char(2)))
set @.Minute = cast(reverse(cast(reverse(@.TimePart) as char(4))) as char(2))
return cast(@.Month + '/' + @.Day + '/' + @.Year + ' ' + @.Hour + ':' + @.Minute + ':' + @.Second as datetime)
end
go
create procedure dbo.sp_OA_CreateFile (
@.FileName nvarchar(2000) = null,
@.FileID int output,
@.fs int output)
as
declare @.result int
exec @.result = sp_OACreate 'Scripting.FileSystemObject', @.fs output
if @.result != 0 begin
raiserror ('Failed to create Scripting.FileSystemObject!', 15, 1)
return (1)
end
exec @.result = sp_OAMethod @.fs, 'CreateTextFile', @.FileID output, @.FileName, 1
if @.result != 0 begin
raiserror ('Failed to create/overwrite specified file!', 15, 1)
return (1)
end
return (0)
GO
create procedure dbo.sp_OA_WriteLineToFile (
@.FileID int,
@.Line varchar(8000) = null)
as
declare @.result int
exec @.result = sp_OAMethod @.FileID, 'WriteLine', null, @.Line
if @.result != 0 begin
raiserror ('Failed to write specified line to file!', 15, 1)
return (1)
end
return (0)
GO
create procedure dbo.sp_OA_CloseFile (
@.FileID int,
@.fs int )
as
declare @.result int
exec @.result = sp_OADestroy @.FileID
if @.result != 0 begin
raiserror ('Failed to close file!', 15, 1)
return (1)
end
exec @.result = sp_OADestroy @.fs
if @.result != 0 begin
raiserror ('Failed to close Scripting.FileSystemObject!', 15, 1)
return (1)
end
return (0)
GO
create procedure dbo.sp_StoreImage2Table (
@.FileName nvarchar(4000) = null,
@.FileDescription varchar (255) = null,
@.TypeID int = 0,
@.CategoryID int = 0,
@.SubCategoryID int = 0)
as
set nocount on
declare @.FileSize int,
@.Path nvarchar(4000),
@.FmtFile nvarchar(4000),
@.cmd varchar(8000),
@.DocumentID int,
@.ptr binary(16),
@.ptr_tmp binary(16),
@.fs int,
@.FileID int,
@.error int
if @.FileName is null begin
raiserror ('File name must be specified!', 15, 1)
return (1)
end
if @.FileDescription is null begin
raiserror ('File description must be specified!', 15, 1)
return (1)
end
if charindex('\', @.FileName) = 0 begin
set @.cmd = 'File name <' + upper(@.FileName) + '> is of invalid name!'
raiserror (@.cmd, 15, 1)
return (1)
end
if @.TypeID = 0 begin --Type of document has noot been specified
raiserror ('Document type is not specified!', 15, 1)
return (1)
end
if @.CategoryID = 0 begin --Document category has noot been specified
raiserror ('Document category is not specified!', 15, 1)
return (1)
end
if @.SubCategoryID = 0 begin --Document Sub-category has noot been specified
raiserror ('Document sub-category is not specified!', 15, 1)
return (1)
end
set @.Path = reverse(substring(reverse(@.FileName), charindex('\', reverse(@.FileName), 1), 4000))
set @.FmtFile = @.Path + 'fmt.fmt'
begin tran
create table #FileInfo (
AlternateName nchar(12) null,
FileSize int null,
CreationDate char(8) null,
CreationTime char(6) null,
LastWrittenDate char(8) null,
LastWrittenTime char(6) null,
LastAccessedDate char(8) null,
LastAccessedTime char(6) null,
Attributes varchar(10) null )
create table #output ([output] varchar(8000) null)
commit tran
insert #FileInfo exec master.dbo.xp_getfiledetails @.FileName
select @.FileSize = FileSize from #FileInfo
if @.FileSize is null begin
set @.cmd = 'Specified file <' + upper(@.FileName) + '> does not exist!'
raiserror (@.cmd, 15, 1)
return (1)
end
-- Create format file
exec dbo.sp_OA_CreateFile @.FmtFile, @.FileID output, @.fs output
exec dbo.sp_OA_WriteLineToFile @.FileID, '8.0'
exec dbo.sp_OA_WriteLineToFile @.FileID, '1'
set @.cmd = "1 " +
space(8) + "SQLIMAGE" + space(8) + "0" + space(8) +
cast(@.FileSize as varchar(25)) + space(8) +
char(34) + char(34) + space(8) + "1" + space(8) +
"fImage" + space(8) + char(34) + char(34)
exec dbo.sp_OA_WriteLineToFile @.FileID, @.cmd
exec dbo.sp_OA_CloseFile @.FileID, @.fs
-- Store binary data to temporary location in the database
if exists (select 1 from dbo.tblImages_tmp (nolock)) delete dbo.tblImages_tmp
set @.cmd = "bulk insert " + db_name() + ".dbo.tblImages_tmp from '" + @.FileName + "' " +
"with (formatfile = '" + @.FmtFile + "')"
exec (@.cmd)
set @.cmd = "exec master.dbo.xp_cmdshell 'del " + @.FmtFile + "'"
insert #output exec (@.Cmd)
if not exists (select 1 from dbo.tblImages_tmp) begin
set @.cmd = 'Error occurred while storing file <' + upper(@.FileName) + '> into a temporary table!'
raiserror (@.cmd, 15, 1)
return (1)
end
select @.DocumentID = DocumentID from dbo.tblDocuments d (nolock)
inner join #FileInfo f on d.AlternateName = f.AlternateName and d.DocumentName = @.FileName
-- Insert new tblDocuments record if it doesn't exist
if @.DocumentID is null begin
begin tran
insert dbo.tblDocuments (
TypeID,
CategoryID,
SubCategoryID,
DocumentName ,
[Description] ,
DocumentSize ,
AlternateName ,
CreattionDate ,
LastModified ,
LastAccessed ,
Attributes )
select @.TypeID, @.CategoryID, @.SubCategoryID,
@.FileName, @.FileDescription, @.FileSize, AlternateName,
dbo.fn_DateFromCharacter(CreationDate, CreationTime),
dbo.fn_DateFromCharacter(LastWrittenDate, LastWrittenTime),
dbo.fn_DateFromCharacter(LastAccessedDate, LastAccessedTime),
Attributes from #FileInfo
select @.DocumentID = scope_identity(), @.error = @.@.error
if @.error != 0 begin
raiserror ('Failed to initialize target table!', 15, 1)
rollback tran
return (1)
end
commit tran
end
-- Initialize IMAGE field with NULL
begin tran
update dbo.tblDocuments set DocumentImage = null where DocumentID = @.DocumentID
if @.@.error != 0 begin
raiserror ('Failed to initialize target field!', 15, 1)
rollback tran
return (1)
end
commit tran
-- Use UPDATETEXT to transfer the image from temporary table
select @.ptr = textptr(DocumentImage) from dbo.tblDocuments where DocumentID = @.DocumentID
select @.ptr_tmp = textptr(fImage) from dbo.tblImages_tmp
begin tran
updatetext tblDocuments.DocumentImage @.ptr 0 null tblImages_tmp.fImage @.ptr_tmp
if @.@.error != 0 begin
raiserror ('Failed to store document into target table!', 15, 1)
rollback tran
return (1)
end
-- Clear the temporary table
delete dbo.tblImages_tmp
if @.@.error != 0 begin
raiserror ('Failed to clear temporary table!', 15, 1)
rollback tran
return (1)
end
commit tran
return (0)
go|||Microsoft says it's not wise http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnproasp/html/binarydata.asp
Here is some samplecode to do it

-- OJ: TEXTCOPY example
-- Loading files into db &
-- exporting files out to folder
--

------
--TEXTCOPY IN
------
--create tb to hold data
create table tmp(fname varchar(100),img image default '0x0')
go
declare @.sql varchar(255),
@.fname varchar(100),
@.path varchar(50),
@.user sysname,
@.pass sysname

set @.user='myuser'
set @.pass='mypass'

--specify desired folder
set @.path='c:\winnt\'

set @.sql='dir ' + @.path + '*.bmp /c /b'

--insert filenames into tb
insert tmp(fname)
exec master..xp_cmdshell @.sql

--loop through and insert file contents into tb
declare cc cursor
for select fname from tmp

open cc
fetch next from cc into @.fname

while @.@.fetch_status=0
begin
set @.sql='textcopy /s"'+@.@.servername+'" /u"'+@.user+'" /p"'+@.pass+'"
/d"'+db_name()+'" /t"tmp" /c"img" /w"where fname=''' + @.fname + '''"'
set @.sql=@.sql + ' /f"' + @.path + @.fname + '" /i' + ' /z'
print @.sql
exec master..xp_cmdshell @.sql ,no_output
fetch next from cc into @.fname
end
close cc
deallocate cc
go
select * from tmp
go
------
--TEXTCOPY OUT
------
declare @.sql varchar(255),
@.fname varchar(100),
@.path varchar(50),
@.user sysname,
@.pass sysname

set @.user='myuser'
set @.pass='mypass,'

--specify desired output folder
set @.path='c:\tmp\'

set @.sql='md ' + @.path

--create output folder
exec master..xp_cmdshell @.sql

--loop through and insert file contents into tb
declare cc cursor
for select fname from tmp

open cc
fetch next from cc into @.fname

while @.@.fetch_status=0
begin
set @.sql='textcopy /s"'+@.@.servername+'" /u"'+@.user+'" /p"'+@.pass+'"
/d"'+db_name()+'" /t"tmp" /c"img" /w"where fname=''' + @.fname + '''"'
set @.sql=@.sql + ' /f"' + @.path + @.fname + '" /o' + ' /z'
print @.sql
exec master..xp_cmdshell @.sql ,no_output
fetch next from cc into @.fname
end
close cc
deallocate cc

set @.sql='dir ' + @.path + '*.bmp /c /b'
exec master..xp_cmdshell @.sql

go
drop table tmp
go|||Please read from the link that you posted and look for the authors (wink-wink, it's not Microsoft!)

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnproasp/html/professionalactiveserverpages.asp|||I see. I guess I assume if it's on MSDN it at least passed some review. A few reasons I can think of not to do it are DB performance in general, complexity of the code and apps to support it, and not being able to do incremental backups on a DB like you can for file system documents. It's an often debated topic. We have had at least 5 database backed commercial document management systems and none of them stored documents in the DB. Does MS's terraserver store images in the DB? (I don't know, just wondering.)|||I think that's the reason why it's called terra server, there is nothing else in it, just images.

Yukon will still support TEXT/NTEXT and IMAGE datatypes, but will introduce VARCHAR/NVARCHAR(MAX) as well.|||I am also working on saving IMAGE to MSSQL. I am using JAVA as language.

Please send me code at kuldips@.bsharp.com|||I would recommend to re-read the previous posts to eliminate further questions, because all the answers that can be possibly given to the questions...have been posted...|||THIS THREAD IS DEAD!

DEAD, DEAD, DEAD!

IT HAS BEEN DEAD FOR A YEAR. NOBODY IS GOING TO SEND YOU ANY HELPFUL SOFTWARE. NOBODY IS GOING TO GIVE YOU ANY USEFUL ADVICE.

By posting to this thread and expecting any sort of helpful reply, you hearby declare yourself a moron of insufficient intelligence to browse the internet. For your own protection from phishers, overseas pharmaceutical marketers, and deposed Nigerian despots, please disconnect your 56K modem NOW!|||Hi there

I have a Stored procedure which stores image in SQL tables , if u are interested let me know , I will send you the code

Regards

Rajesh
Plz send the code to me too
thanks
my mail is :
yosif4444@.gmail.com|||yosif4444, you have hereby declared yourself a moron of the highest degree. A poster of insufficient intelligence to browse the internet. Please remove your coffee cup from your CD Drive tray, wipe the white-out off of your screen, unplug your computer, and go outside and play in the driveway.|||This sounds very useful.
Please message me with the code.
Many thanks.
DbaJames.|||This thread collects idiots like flypaper collects flies. DbaJames, you seem to have something stuck to your shoe...|||OK, I'm locking this thread to prevent others from displaying their ignorance.

How to insert data into two tables simulatneously, using Stored Procedure?

Hi all,
I have heard that we must insert into two tables simultaneously when there is a ONE-TO-ONE relationship.

Can anyone tell me how insert into two tables at the same time, using SP?

Thanks

Tomy

tomyseb:


I have heard that we must insert into two tables simultaneously when there is a ONE-TO-ONE relationship.

Not true.

The fact is that you can't insert into 2 tables simultaneously. Commands are executed in a procedural fashion.

Also, it extremely rare to need a one-to-one relationship. A typical example of when it is useful is if you would otherwise be in danger of going over the maximum number of fields limit in Access, and need to break the table up.

sql

how to insert data in to two tables?

hi,

i have got a problem, iam creating an asp.net application, but i pretend to insert data in two different tables, but i have no ideia how to do this.Can you help to solve this problem? please........

So, you need to insert data into two different tables? What database are you using? If you are using SQL or MSDE, you would use 2 insert statements or a stored procedure.|||

scosta wrote:

hi,

i have got a problem, iam creating an asp.net application, but ipretend to insert data in two different tables, but i have no ideia howto do this.Can you help to solve this problem? please........


This is as simple as executing two SQL INSERTS in sequence or as complicated as writing a stored proc that does the same thing.
Time to pick up an introductory book on SQL Server and ADO.NET my friend.
|||

iam using Sql, and i want to use the way to solve the problem, or 2 insert statements or a stored procedure.

how to insert data in to two tables?

hi,

iam creating an asp.net application the database is created on the SQLSERVER, but i have a problem, i pretend to insert data in to 2 tables and i have no ideia how to do this. Can you help me to solve this problem? Please....

you would need 2 insert statements. check out books online for syntax on INSERT. You could also use stored procedures to have all the inserts in it so you can get your job done in one trip to the server.sql

Monday, March 26, 2012

How to Index through a tables Columns

I am trying to index through the columns of MyTable so I can do the same work on all columns. I know how to get the column names from MyTable but when I use @.MyColName in the SELECT statement to get MyTable Column 0 Row values I get a table with the column name in each row cell. I can't get the syntax correct to return the value in each cell for that column.

This is a extremely simplified example !!!!!!
DECLARE @.MyColName nvarchar(30)

--Get the MyTable Column 0 Name
SELECT @.MyColName = Col_Name(Object_ID('MyTable'), 0)

--Display the MyTable Column 0 Row values
SELECT @.MyColName FROM MyTable --This is the syntax I can not get correct

Can anyone help ?

Thanks

You can't use a variable for a column name; you will have to usedynamic SQL to meet your goal. Build the SQL statement in avariable and then EXECUTE it, like this:
DECLARE @.MySQLStatement varchar(500)
SELECT @.MySQLStatement = 'SELECT ' + @.MyColName + ' FROM MyTable'
EXECUTE(@.MySQLStatement)


|||

tmorton

Thanks for your responce, I will give it a try.

Wednesday, March 21, 2012

How to Include months not in the records?

Hi,
I have 2 tables created.
One is the Sale Persons (PersonID, PersonName) and the other is the Sales Detail (PersonID, Month, Year, TotalSales)
PersonID | PersonName PersonID | Month | Year | Total Sales
================== ==================================
ID1 | Sally ID1 | 1 | 2005 | 1000
ID2 | David ID2 | 2 | 2005 | 1500
Is it possible to write in a SQL statement to return all the sales person & the total sales for the 12 months for a particular year (even though some month data are not in the table)
I would like the result to be like the following:
PersonID | Month | Year | Total Sales
===================================
ID1 | 1 | 2005 | 1000
ID1 | 2 | 2005 | 0
ID1 | 3 | 2005 | 0
(For month 4 - 12) Total Sales will be 0 too as no records exist in the first place
ID2 | 1 | 2005 | 0
ID2 | 2 | 2005 | 1500
........

Do you have to do it in SQL or can you do it in code?|||

Create a temporary table using a sql proc or in code that contains the year(s) and months that you do want to include. Then you just have to OUTER join it to your existing tables to fill in the missing gaps.
HTH.

How to include data from 2 unlinked tables

I am using VB6 and CR10 & SQL Server 7
I am working on an inventory project nw.. I'm new to crystal reports..

For the report i need to pick data from 2 tables(Sales_Details, Purchase_Details)
There is no link between these 2 tables (except a similarity that both have got a date field)

TABLE 1
Sale_Date... SalesQty
01/01/07...... 50
01/03/07...... 24
01/04/07...... 10

TABLE 2
Purch_Date... Purchase Qty
01/01/07 ....... 100
01/06/07 ....... 100

I need to take a report which shows the movement of a particular Item based on date range
It should look like this

------x----x----x
Date ......... Sales.......Purchase
------x----x----x

01/01/07 ........ 50 ........ 100
01/03/07 ........ 24 ........ 0
01/04/07 ........ 10 ........ 0
01/06/07 ........ 0 ........ 100
--------------
I tried inner and outer joins to links these tables, but all in vein( data is getting repeated then), tried with groups also..no use

Pleeeaaase... help me to do this...
This may be a simple issue for most of you...
But i am a beginner.. i need your valuable help..
Plzzzzz...Just to make sure, but there are no PKeys in either one of your tables?|||You say you need to report by item, so surely there must be some sort of item code in both tables otherwise how do you know what item you're buying / selling?|||Ofcourse item code is there... For sake of simplicity i dint mention that.

Actually both the purchase and sales table has the fileds- Date, Itemcode, Quantity

Sales Table

Date...... ItemCode........Qnty
------------
01/01/07 ..A001............50
01/03/07...A001............24
01/04/07...A001............10

Purchase Table
Date...... ItemCode........Qnty
------------
01/01/07...A001............100
01/06/07...A001............100

and i need a report (like the one mentioned above)based on the movement of Item -A001 ..

Please...........|||First:
You wrote:

There is no link between these 2 tables (except a similarity that both have got a date field)
This is the reason for the questions now I assume itemcode is your link.
Next you will group by date and itemcode if you want, now for grouping by date go to options and choose section to be printed by day.
Then you will need to create formulas that will check to see the quantity of sales and purchases.

Something like:

If {Sales.quantity} > 0
Then {Sales.quantity}
Else 0

Hope that helps,
GJ

How to improve the performance of a query?

I have a table called DMPD_Product_Lookup_Dom. It is a lookup table which contains values for certain fields of other tables in the database.
This takes long time to run.
Is there any way to improve performance of this query ??

SELECT
BNAD.Benefit_Admin_Cat_CD AS 'AdminCategory',
DOM1.Value_Description AS 'AdminCategoryDesc',
BNAD.Benefit_Component_Name_CD AS 'BenefitAdmin',
DOM2.Value_Description AS 'BenefitAdminDesc',
BNDT.Benefit_Rider_CD AS 'BenefitRider',
DOM3.Value_Description AS 'BenefitRiderDesc',
BNDT.Benefit_Exception_Ind AS 'Exception',
BNDT.Benefit_Detail_Desc_CD AS 'BenefitDetail',
DOM4.Value_Description AS 'BenefitDetailDesc',
DMCS.Cost_Share_Value_Type_CD AS 'CSValueType',
DOM5.Value_Description AS 'CSValueTypeDesc',
DMCS.Cost_Share_Value AS 'CS_Value',
DMCS.Cost_Share_Rule_Cat_CD AS 'CS_Rule_Cat_CD',
DOM6.Value_Description AS 'CS_Rule_Cat_Value',
BNCS.Cost_Share_Rule_Type_CD AS 'CS_Rule_Type_CD',
DOM7.Value_Description AS 'CSRuleTypeDesc',
BNCS.Cost_Share_Mbr_Family_Ind AS 'MemberOrFamily',
DOM8.Value_Description AS 'MemberOrFamilyDesc',
BNCS.Network_Ind AS 'NetworkInd'
FROM
prdtrk01..DMPD_Product_Lookup_Dom DOM1,
prdtrk01..DMPD_Product_Lookup_Dom DOM2,
prdtrk01..DMPD_Product_Lookup_Dom DOM3,
prdtrk01..DMPD_Product_Lookup_Dom DOM4,
prdtrk01..DMPD_Product_Lookup_Dom DOM5,
prdtrk01..DMPD_Product_Lookup_Dom DOM6,
prdtrk01..DMPD_Product_Lookup_Dom DOM7,
prdtrk01..DMPD_Product_Lookup_Dom DOM8,
prdtrk01..BNAD_Benefit_Admin BNAD,
prdtrk01..BNDT_Benefit_Detail BNDT,
prdtrk01..BNCS_Cost_Share_Rule BNCS,
prdtrk01..DMCS_Cost_Share_Dom DMCS
WHERE
BNAD.Benefit_Admin_ID = BNCS.Benefit_Admin_ID
AND BNDT.Benefit_Detail_ID = BNCS.Benefit_Detail_ID
AND DMCS.Cost_Share_Rule_ID = BNCS.Cost_Share_Rule_ID
AND DOM1.Product_Domain_Entity = "BNAD"
AND DOM1.Product_Attribute_Type = "Benefit_Admin_Cat_CD"
AND DOM1.Domain_Value = BNAD.Benefit_Admin_Cat_CD
AND DOM2.Product_Domain_Entity = "BNAD"
AND DOM2.Product_Attribute_Type = "Benefit_Component_Name_CD"
AND DOM2.Domain_Value = BNAD.Benefit_Component_Name_CD
AND DOM3.Product_Domain_Entity = "BNDT"
AND DOM3.Product_Attribute_Type = "Benefit_Rider_CD"
AND BNDT.Benefit_Rider_CD *= DOM3.Domain_Value
AND DOM4.Product_Domain_Entity = "BNDT"
AND DOM4.Product_Attribute_Type = "Benefit_Detail_Desc_CD"
AND DOM4.Domain_Value = BNDT.Benefit_Detail_Desc_CD
AND DOM5.Product_Domain_Entity = "DMCS"
AND DOM5.Product_Attribute_Type = "Cost_Share_Value_Type_CD"
AND DOM5.Domain_Value = DMCS.Cost_Share_Value_Type_CD
AND DOM6.Product_Domain_Entity = "DMCS"
AND DOM6.Product_Attribute_Type = "Cost_Share_Rule_Cat_CD"
AND DOM6.Domain_Value = DMCS.Cost_Share_Rule_Cat_CD
AND DOM7.Product_Domain_Entity = "BNCS"
AND DOM7.Product_Attribute_Type = "Cost_Share_Rule_Type_CD"
AND DOM7.Domain_Value = BNCS.Cost_Share_Rule_Type_CD
AND DOM8.Product_Domain_Entity = "BNCS"
AND DOM8.Product_Attribute_Type = "Cost_Share_Mbr_Family_Ind"
AND DOM8.Domain_Value = BNCS.Cost_Share_Mbr_Family_Ind
AND BNCS.Product_ID = @.Product_ID
ORDER BY
DOM1.Sort_Seq_No,
DOM1.Value_Description,
DOM2.Sort_Seq_No,
DOM2.Value_Description,
DOM3.Sort_Seq_No,
DOM3.Value_Description,
DOM4.Sort_Seq_No,
DOM4.Value_Description,
DOM5.Sort_Seq_No,
DOM5.Value_Description,
DOM6.Sort_Seq_No,
DOM6.Value_Description,
DOM7.Sort_Seq_No,
DOM7.Value_Description,
DOM8.Sort_Seq_No,
DOM8.Value_DescriptionDo you have indexes on these tables?|||

Quote:

Originally Posted by iburyak

Do you have indexes on these tables?


Yes, the tables mentioned have indexes. But i want to know is there any other way to improve the performance of this query.|||Unfortunately you didn't provide specific answer.
To help you I need to see all index compositions.
Some people think they have indexes but it could be that they are not the once that such query will use.

To view if your query uses indexes in SQL Query Analyzer go to Query Show Execution Plan.
Execute your query and point to each item and see if your indexes are actually used.|||well the performance can be boosted using the appropriate filters in the FROM clause itself. Try to use INNER and the other different appropriate joins in the clause.The default join is the CROSS join which is the worst join. So try to filter out the results as much as possible in the FROM clause. Definitely the performance will be boosted up to some instant.|||

Quote:

Originally Posted by abhishek8236

well the performance can be boosted using the appropriate filters in the FROM clause itself. Try to use INNER and the other different appropriate joins in the clause.The default join is the CROSS join which is the worst join. So try to filter out the results as much as possible in the FROM clause. Definitely the performance will be boosted up to some instant.


Thanks Everyone! The Problem has been resolved.

How to improve the efficience of Sql query ?

now i want to get results from server tables, but i found it is very slow, for example :

select Coalesce(T1.Name, T2.Name, T3.Name), T1.M1, T2.M2, T3.M3

from T1

full outer join T2

on Coalesce(T1.Name, NULL) = T2.Name

full outer join T3

on Coalesce(T1.Name, T2.Name) = T3.Name

in Tables i have builded index for name, but when every table have 20000 records, the sql above is very slow, is there other method to improve the query speed ?

Thks

You're trying to full outer join three tables and on the join condition is based on computed columns... well that's a query not build for performance...
If you want to improve performance you need to rewrite the query in a way that doesn't uses coalesce on the join condition, you should use tables columns directly and then filter on the where or on the having

Just a final thought: The
Coalesce(T1.Name, NULL) = T2.Name
Coalesce(T1.Name, T2.Name) = T3.Name
conditions may be rewritten as
T2.Name = Coalesce(T1.Name, NULL)
T3.Name = Coalesce(T1.Name, T2.Name)
take a look if this changes the execution plan.
|||

Liu:

Do you filter based on name or are you wanting to process all rows of all tables?

Dave

|||

every one:

i have solved this problem with hash join such as belows:

select Coalesce(T1.Name, T2.Name, T3.Name), T1.M1, T2.M2, T3.M3

from T1

full outer hash join T2

on Coalesce(T1.Name, NULL) = T2.Name

full outer hash join T3

on Coalesce(T1.Name, T2.Name) = T3.Name

in Tables i have builded index for name, but when every table have 20000 records, the sql above is very slow, is there other method to improve the query speed ?

the time to query changes from 33m to 4s

thks

How to improve performance if inner join has more than 2 or 3 tables

Hi everyone

I need a solution for this query. It is working fine for 2 tables but when there are 1000's of records in each table and query has more than 2 tables. The process never ends.

Here is the query

(select siqPid= 1007, t1.Gmt909Time as GmtTime,(t1.engValue+t2.engValue+t3.engValue+t4.engValue) as EngValue,

t1.Loc1Time as locTime,t1.msgId

into #temp5

from #temp1 as t1,#temp2 as t2,#temp3 as t3,#temp4 as t4

where t1.Loc1Time = t2.Loc1Time and t2.Loc1Time = t3.Loc1Time and t3.Loc1Time = t4.Loc1Time)

I was trying to do something with this query.

But the engValues cant be summed up. and if I add that in the query, the query isnt compiling.

(select siqPid= 1007, t1.Gmt909Time as GmtTime,

t1.Loc1Time as locTime,t1.msgId,(t1.engValue+t2.engValue+t3.engValue+t4.engValue) as engValue

--into #temp5

from #temp1 as t1

where exists

(Select 1

from #temp2 as t2

where t1.Loc1Time = t2.Loc1Time and

exists

(Select 1

from #temp3 as t3

where t2.Loc1Time = t3.Loc1Time and

exists

(Select 1

from #temp4 as t4

where t3.Loc1Time = t4.Loc1Time))))

I need immediate help on that, I would appreciate an input on it.

Thanks

-Sarah

What do you mean by the engValues can't be added? You shouldn't have problems joining 4 tables with 1000's of records. SQL Server is designed for queries involving orders of magnitude more records. Here are some suggestions:

1. Use better join syntax. Instead of FROM A, B WHERE A.ID = B.ID use FROM A INNER JOIN B ON A.ID = B.ID.

2. Read up on Table Variables and use them instead of your temp tables. Here's a good link to read about them:http://odetocode.com/Articles/365.aspx. In particular, note that you can define primary keys for table variables which should assist in your query performance.

HTH

|||

Thanks ZLA for the input

The problem is there is already alot of work done before and temp tables are crreated already. So I need to use those for this project.

Can you please help me in refining the query. I did use the syntax you asked me, but its still taking too long. Something like this

Select t1.engValue+t2.engValue+t3.engValue+t4.engValue
from #temp5 t5
inner join #temp1 t1
on t1.Loc1Time = t5.locTime
inner join #temp2 t2
on t2.Loc1Time = t5.locTime
inner join #temp3 t3
on t3.Loc1Time = t5.locTime
inner join #temp4 t4
on t4.Loc1Time = t5.locTime

but that too is taking too long... Please help me with this..

|||

Have you looked at the estimated or actual execution plans for your query. In general, table scans perform slow and I suspect you have too many of them in your execution plan. It is usually better if the plan makes use of indexes.

You can create temp tables with identities, primary keys and indexes. Here is a good article that shows how:http://www.sqlteam.com/item.asp?ItemID=15442 Even though you are stuck with the temp tables, can you at least add some indexes to them to speed up the query execution?

There are other alternatives as well; you might try thinking laterally. Perhaps you can update temp table 5 (or use a separate temp table 6) where you insert 1 row from each of the other 4 temp tables. Just insert locTime, engValue. Then do the following:

SELECT locTime, Sum(engValue) From #Temp6 GROUP BY locTime HAVING Count(*) = 4

This gives the sum but only where all four tables have inserted a record (equivalent to locTime being present in all four temp tables which your inner joins select for).

HTH

|||

Hi ZLA

Thats article has very good points, I can incorporate that in my process but I need to get rid of the error also in this query. Can you please help me in finding out why am I getting an error on this line and what can I do to make it work.

,(t1.engValue+t2.engValue+t3.engValue+t4.engValue) as engValue

(select siqPid= 1007, t1.Gmt909Time as GmtTime,

t1.Loc1Time as locTime,t1.msgId,(t1.engValue+t2.engValue+t3.engValue+t4.engValue) as engValue

--into #temp5

from #temp1 as t1

where exists

(Select 1

from #temp2 as t2

where t1.Loc1Time = t2.Loc1Time and

exists

(Select 1

from #temp3 as t3

where t2.Loc1Time = t3.Loc1Time and

exists

(Select 1

from #temp4 as t4

where t3.Loc1Time = t4.Loc1Time))))

You suggestions are awasome, and the last suggestion that you gave did actually worked for me, but I still want to know why the where exisit query doesnt work, if I want to add the column values from the later tables.

I would appreciate your help on this.

-Sarah

|||

Is it in this last query you are still getting an error? What error are you getting? Even without that, I suspect the problem is because not all the fields exist. Using the exists clause, your query is essentially:

select siqPid= 1007, t1.Gmt909Time as GmtTime, t1.Loc1Time as locTime,t1.msgId,(t1.engValue+t2.engValue+t3.engValue+t4.engValue) as engValue

from #temp1 as t1

where exists ( ... )

This select statement only pulls selected fields from #temp1. Therefore t2, t3, t4 aren't available. They are only used in the WHERE clause. You'll need to go back to a join or use my prior idea of inserting their individual results into a new temp table (#temp6). Then sum across temp6 into temp5.

|||

How long is it taking?

How long is "too long"?

How many rows does each of the temp tables have?

|||

Yes, the inner join is actually never shows up any results, it says its processing the query... I ran it with 282 records each in 4 tables, and the process never got completed, so talking about 1000 of records in each tables it far. But I will be doing that eventually, so I dont know whats wrong with the inner join statement too.

ZLA, yes, that makes sense, the exists clause probably is doing that. And the sum does work with teh group by statement. So for right now I have a solution that you suggested to sum them up. For one time its fine, but not always... as I had to put extra three query's instead of putting just one inner join query.

But to avoid future errors, is the where clause or on clause in the inner join query asking for too much, 'cause there is no clear filter like loctime = 5/6/1990...

Please let me know how else can I improve my inner join then..

Thanks

-Sarah

|||

Am sorry, I was talking about this query now in my last post

Select t1.engValue+t2.engValue+t3.engValue+t4.engValue
from #temp5 t5
inner join #temp1 t1
on t1.Loc1Time = t5.locTime
inner join #temp2 t2
on t2.Loc1Time = t5.locTime
inner join #temp3 t3
on t3.Loc1Time = t5.locTime
inner join #temp4 t4
on t4.Loc1Time = t5.locTime

|||

I started to try to test this locally by creating my own test tables and realized one possible cause for your query to take so long, even with so few records. How many duplicate locTime / Loc1Time values are there across the 5 temp tables?

For example, assume the same datetime value appears 10 times in each table. Since you are only joining on the datetime field, temp5 joined to temp1 will produce 10 x 10 = 100 result rows. Since you have 5 tables, 10 duplicates could produce 100,000 rows by themselves. If your actual number of duplicates is higher than my example, you could be trying to run a query that will return a million or more records.

Try running the following to test this:

SELECT locTime, Count(*) FROM #temp5 GROUP BY locTime HAVING Count(*) > 1 ORDER BY Count(*) DESC

SELECT loc1Time, Count(*) FROM #temp1 GROUP BY loc1Time HAVING Count(*) > 1 ORDER BY Count(*) DESC

etc...

Let me know what you find out.

|||

Well, I went ahead and tested this locally. For my test, I was able to join the tables, each with only 3 duplicates, to return 74,500 rows in less than a second. Below is all of the SQL I used to test this. If you are still seeing the performance problems, it makes me think either the server is way overloaded or there is some other resource constraint impeding your efforts.

Test Table Creation:

ifexists (select *from dbo.sysobjectswhere id =object_id(N'[dbo].[T5]')andOBJECTPROPERTY(id, N'IsUserTable') = 1)drop table [dbo].[T5]GOifexists (select *from dbo.sysobjectswhere id =object_id(N'[dbo].[T1]')andOBJECTPROPERTY(id, N'IsUserTable') = 1)drop table [dbo].[T1]GOifexists (select *from dbo.sysobjectswhere id =object_id(N'[dbo].[T2]')andOBJECTPROPERTY(id, N'IsUserTable') = 1)drop table [dbo].[T2]GOifexists (select *from dbo.sysobjectswhere id =object_id(N'[dbo].[T3]')andOBJECTPROPERTY(id, N'IsUserTable') = 1)drop table [dbo].[T3]GOifexists (select *from dbo.sysobjectswhere id =object_id(N'[dbo].[T4]')andOBJECTPROPERTY(id, N'IsUserTable') = 1)drop table [dbo].[T4]GOCREATE TABLE [dbo].[T5] ([ID] [int]IDENTITY (1, 1)NOT NULL ,[locTime] [datetime]NOT NULL )ON [PRIMARY]GOCREATE TABLE [dbo].[T1] ([ID] [int]IDENTITY (1, 1)NOT NULL ,[loc1Time] [datetime]NOT NULL ,[engValue] [float]NOT NULL )ON [PRIMARY]GOCREATE TABLE [dbo].[T2] ([ID] [int]IDENTITY (1, 1)NOT NULL ,[loc1Time] [datetime]NOT NULL ,[engValue] [float]NOT NULL )ON [PRIMARY]GOCREATE TABLE [dbo].[T3] ([ID] [int]IDENTITY (1, 1)NOT NULL ,[loc1Time] [datetime]NOT NULL ,[engValue] [float]NOT NULL )ON [PRIMARY]GOCREATE TABLE [dbo].[T4] ([ID] [int]IDENTITY (1, 1)NOT NULL ,[loc1Time] [datetime]NOT NULL ,[engValue] [float]NOT NULL )ON [PRIMARY]GOALTER TABLE [dbo].[T5]WITH NOCHECK ADD CONSTRAINT [PK_T5]PRIMARY KEY CLUSTERED ([ID])ON [PRIMARY] GOALTER TABLE [dbo].[T1]WITH NOCHECK ADD CONSTRAINT [PK_T1]PRIMARY KEY CLUSTERED ([ID])ON [PRIMARY] GOALTER TABLE [dbo].[T2]WITH NOCHECK ADD CONSTRAINT [PK_T2]PRIMARY KEY CLUSTERED ([ID])ON [PRIMARY] GOALTER TABLE [dbo].[T3]WITH NOCHECK ADD CONSTRAINT [PK_T3]PRIMARY KEY CLUSTERED ([ID])ON [PRIMARY] GOALTER TABLE [dbo].[T4]WITH NOCHECK ADD CONSTRAINT [PK_T4]PRIMARY KEY CLUSTERED ([ID])ON [PRIMARY] GO

Test Table Population:

DECLARE @.Counterint, @.Datedatetime, @.Table intSET @.Counter = 0SET @.Date ='01/01/2001 00:00'WHILE (@.Counter < 1000)BEGIN IF @.Counter = 350or @.Counter = 700BEGIN SET @.Date ='01/01/2001 00:00'END INSERT T5 (locTime)VALUES (@.Date)SET @.Counter = @.Counter + 1SET @.Date =DateAdd(hh, 1, @.Date)ENDSET @.Table = 1WHILE (@.Table < 5)BEGIN SET @.Counter = 0SET @.Date ='01/01/2001 00:00'WHILE (@.Counter < 1000)BEGIN IF @.Counter = 350or @.Counter = 700BEGIN SET @.Date ='01/01/2001 00:00'END IF @.Table = 1INSERT T1 (loc1Time, engValue)VALUES (@.Date, rand())ELSE IF @.Table = 2INSERT T2 (loc1Time, engValue)VALUES (@.Date, rand())ELSE IF @.Table = 3INSERT T3 (loc1Time, engValue)VALUES (@.Date, rand())ELSE IF @.Table = 4INSERT T4 (loc1Time, engValue)VALUES (@.Date, rand())SET @.Counter = @.Counter + 1SET @.Date =DateAdd(hh, 1, @.Date)END SET @.Table = @.Table + 1ENDselectcount(*)from T5selectcount(*)from T1selectcount(*)from T2selectcount(*)from T3selectcount(*)from T4

Temp Table Join Test:

SELECT *INTO #temp5FROM T5SELECT *INTO #temp1FROM T1SELECT *INTO #temp2FROM T2SELECT *INTO #temp3FROM T3SELECT *INTO #temp4FROM T4Select t1.engValue+t2.engValue+t3.engValue+t4.engValuefrom #temp5 t5innerjoin #temp1 t1on t1.Loc1Time = t5.locTimeinnerjoin #temp2 t2on t2.Loc1Time = t5.locTimeinnerjoin #temp3 t3on t3.Loc1Time = t5.locTimeinnerjoin #temp4 t4on t4.Loc1Time = t5.locTimeDROP TABLE #temp5DROP TABLE #temp1DROP TABLE #temp2DROP TABLE #temp3DROP TABLE #temp4
|||

Hi ZLA

Yes, that can be a possiblity because for certain locValues, I have been having probably 20-30 duplicate locId's that must be the cause of keeping the process busy.

I still need to go over the table example you showed me, I will go over it tomorrow, its been very busy at work today... I am really thankful to you for this help.

Tomorrow I will go over it and will create more duplicate records and will confirm if thats the reason of my problem.

Thanks once again

-Sarah

|||

That sounds like it's the cause, then. If you have trouble figuring out how to handle the duplicates, let me know. You'll need to decide / know what the correct answer should be. For example, if

Temp1.loc1Time = '7/1/2006 12:34', Temp1.engValue = 1.0

Temp1.loc1Time = '7/1/2006 12:34', Temp1.engValue = 2.0

Temp2.loc1Time = '7/1/2006 12:34', Temp2.engValue = 0.1

Temp2.loc1Time = '7/1/2006 12:34', Temp2.engValue = 0.2

Assume all other matching records have engValue of 0.0. Using joins, you would get 4 matches: 1.0 and 0.1, 1.0 and 0.2, 2.0 and 0.1, 2.0 and 0.2. That would give you 4 separate sums for that time: 1.1, 1.2, 2.1 and 2.2. If those four are summed together, the grand total is 6.6.

If, however, the grand total for that time should just be 3.3, then the joins are not the right method.

Let me know what you find out.

|||

Hi ZLA

Yes, thats exactly whats happening, one table has 100 adn another has 98 rows with loc1time as 1/1/1900. and with teh join, when I stop in a matter of seconds its showing 14,000 rows and its still not complete.

Yes, it seems like its giving the result of 6.6 and we want 3.3. Either we need to group by the loc1time in each table adn make sure that there is one record in each of the temp tables or delete teh enteries from teh temp tables that have duplicate joining condition.

I think we are going towards your first solution, which asked for grouping in the fifth table with the expression and summing the values up.

what do you suggest?

-Sarah

|||

Hurrah! I can think of several approaches:

1. Is the 1/1/1900 date valid? If not, either correct the underlying problem or exclude these records from the query. To exclude them, you could eliminate them when creating #temp1, #temp2, ... Or you could add a general where clause to the multiple inner join such as "WHERE temp1.loc1Time <> '1/1/1900' AND temp2loc1Time <> '1/1/1900' ..." Or you could modify the joins to do the filtering: "FROM #temp5 t5 INNER JOIN #temp1 t1 ON t1.Loc1Time = t5.locTime AND t1.Loc1Time <> '1/1/1900' ..." I think the first is best if possible; the last is next best.

2. If duplicate values are not correct, I would recommend building the temp tables so they only hold the distinct values you want. That should be the most efficient. If possible, you could get that by adding DISTINCT to the select statement that builds them. If you can't alter the temp table creation, you can use sub-queries in your join:

Select t1.engValue+t2.engValue+t3.engValue+t4.engValue
from (select distinct locTime from #temp5) t5
innerjoin (select distinct Loc1Time from #temp1) t1
on t1.Loc1Time = t5.locTime
innerjoin (select distinct Loc1Time from #temp2) t2
on t2.Loc1Time = t5.locTime
innerjoin (select distinct Loc1Time from #temp3) t3
on t3.Loc1Time = t5.locTime
innerjoin (select distinct Loc1Time from #temp4) t4
on t4.Loc1Time = t5.locTime

3. Use a summing technique on a separate temp table as I described earlier:

There are other alternatives as well; you might try thinking laterally. Perhaps you can update temp table 5 (or use a separate temp table 6) where you insert 1 row from each of the other 4 temp tables. Just insert locTime, engValue. Then do the following: "SELECT locTime, Sum(engValue) From #Temp6 GROUP BY locTime HAVING Count(*) = 4". Please note that the count assumes only 1 row [datetime] is inserted from each temp table. If not, you will need to alter this approach.

The summing technique would be best if a temp table may not have a datetime that is in one of the other temp tables. That would be dependent on your process. If you only want sums where the datetime is in all 5 tables, then I would use a join. If you want all datetimes then I would look at a summing technique.

HTH. Let me know how it turns out.

Monday, March 19, 2012

How to improve performance if inner join has more than 2 or 3 tables

Hi everyone

I need a solution for this query. It is working fine for 2 tables but when there are 1000's of records in each table and query has more than 2 tables. The process never ends.

Here is the query

(select siqPid= 1007, t1.Gmt909Time as GmtTime,(t1.engValue+t2.engValue+t3.engValue+t4.engValue) as EngValue,

t1.Loc1Time as locTime,t1.msgId

into #temp5

from #temp1 as t1,#temp2 as t2,#temp3 as t3,#temp4 as t4

where t1.Loc1Time = t2.Loc1Time and t2.Loc1Time = t3.Loc1Time and t3.Loc1Time = t4.Loc1Time)

I was trying to do something with this query.

But the engValues cant be summed up. and if I add that in the query, the query isnt compiling.

(select siqPid= 1007, t1.Gmt909Time as GmtTime,

t1.Loc1Time as locTime,t1.msgId,(t1.engValue+t2.engValue+t3.engValue+t4.engValue) as engValue

--into #temp5

from #temp1 as t1

where exists

(Select 1

from #temp2 as t2

where t1.Loc1Time = t2.Loc1Time and

exists

(Select 1

from #temp3 as t3

where t2.Loc1Time = t3.Loc1Time and

exists

(Select 1

from #temp4 as t4

where t3.Loc1Time = t4.Loc1Time))))

I need immediate help on that, I would appreciate an input on it.

Thanks

-Sarah

What do you mean by the engValues can't be added? You shouldn't have problems joining 4 tables with 1000's of records. SQL Server is designed for queries involving orders of magnitude more records. Here are some suggestions:

1. Use better join syntax. Instead of FROM A, B WHERE A.ID = B.ID use FROM A INNER JOIN B ON A.ID = B.ID.

2. Read up on Table Variables and use them instead of your temp tables. Here's a good link to read about them:http://odetocode.com/Articles/365.aspx. In particular, note that you can define primary keys for table variables which should assist in your query performance.

HTH

|||

Thanks ZLA for the input

The problem is there is already alot of work done before and temp tables are crreated already. So I need to use those for this project.

Can you please help me in refining the query. I did use the syntax you asked me, but its still taking too long. Something like this

Select t1.engValue+t2.engValue+t3.engValue+t4.engValue
from #temp5 t5
inner join #temp1 t1
on t1.Loc1Time = t5.locTime
inner join #temp2 t2
on t2.Loc1Time = t5.locTime
inner join #temp3 t3
on t3.Loc1Time = t5.locTime
inner join #temp4 t4
on t4.Loc1Time = t5.locTime

but that too is taking too long... Please help me with this..

|||

Have you looked at the estimated or actual execution plans for your query. In general, table scans perform slow and I suspect you have too many of them in your execution plan. It is usually better if the plan makes use of indexes.

You can create temp tables with identities, primary keys and indexes. Here is a good article that shows how:http://www.sqlteam.com/item.asp?ItemID=15442 Even though you are stuck with the temp tables, can you at least add some indexes to them to speed up the query execution?

There are other alternatives as well; you might try thinking laterally. Perhaps you can update temp table 5 (or use a separate temp table 6) where you insert 1 row from each of the other 4 temp tables. Just insert locTime, engValue. Then do the following:

SELECT locTime, Sum(engValue) From #Temp6 GROUP BY locTime HAVING Count(*) = 4

This gives the sum but only where all four tables have inserted a record (equivalent to locTime being present in all four temp tables which your inner joins select for).

HTH

|||

Hi ZLA

Thats article has very good points, I can incorporate that in my process but I need to get rid of the error also in this query. Can you please help me in finding out why am I getting an error on this line and what can I do to make it work.

,(t1.engValue+t2.engValue+t3.engValue+t4.engValue) as engValue

(select siqPid= 1007, t1.Gmt909Time as GmtTime,

t1.Loc1Time as locTime,t1.msgId,(t1.engValue+t2.engValue+t3.engValue+t4.engValue) as engValue

--into #temp5

from #temp1 as t1

where exists

(Select 1

from #temp2 as t2

where t1.Loc1Time = t2.Loc1Time and

exists

(Select 1

from #temp3 as t3

where t2.Loc1Time = t3.Loc1Time and

exists

(Select 1

from #temp4 as t4

where t3.Loc1Time = t4.Loc1Time))))

You suggestions are awasome, and the last suggestion that you gave did actually worked for me, but I still want to know why the where exisit query doesnt work, if I want to add the column values from the later tables.

I would appreciate your help on this.

-Sarah

|||

Is it in this last query you are still getting an error? What error are you getting? Even without that, I suspect the problem is because not all the fields exist. Using the exists clause, your query is essentially:

select siqPid= 1007, t1.Gmt909Time as GmtTime, t1.Loc1Time as locTime,t1.msgId,(t1.engValue+t2.engValue+t3.engValue+t4.engValue) as engValue

from #temp1 as t1

where exists ( ... )

This select statement only pulls selected fields from #temp1. Therefore t2, t3, t4 aren't available. They are only used in the WHERE clause. You'll need to go back to a join or use my prior idea of inserting their individual results into a new temp table (#temp6). Then sum across temp6 into temp5.

|||

How long is it taking?

How long is "too long"?

How many rows does each of the temp tables have?

|||

Yes, the inner join is actually never shows up any results, it says its processing the query... I ran it with 282 records each in 4 tables, and the process never got completed, so talking about 1000 of records in each tables it far. But I will be doing that eventually, so I dont know whats wrong with the inner join statement too.

ZLA, yes, that makes sense, the exists clause probably is doing that. And the sum does work with teh group by statement. So for right now I have a solution that you suggested to sum them up. For one time its fine, but not always... as I had to put extra three query's instead of putting just one inner join query.

But to avoid future errors, is the where clause or on clause in the inner join query asking for too much, 'cause there is no clear filter like loctime = 5/6/1990...

Please let me know how else can I improve my inner join then..

Thanks

-Sarah

|||

Am sorry, I was talking about this query now in my last post

Select t1.engValue+t2.engValue+t3.engValue+t4.engValue
from #temp5 t5
inner join #temp1 t1
on t1.Loc1Time = t5.locTime
inner join #temp2 t2
on t2.Loc1Time = t5.locTime
inner join #temp3 t3
on t3.Loc1Time = t5.locTime
inner join #temp4 t4
on t4.Loc1Time = t5.locTime

|||

I started to try to test this locally by creating my own test tables and realized one possible cause for your query to take so long, even with so few records. How many duplicate locTime / Loc1Time values are there across the 5 temp tables?

For example, assume the same datetime value appears 10 times in each table. Since you are only joining on the datetime field, temp5 joined to temp1 will produce 10 x 10 = 100 result rows. Since you have 5 tables, 10 duplicates could produce 100,000 rows by themselves. If your actual number of duplicates is higher than my example, you could be trying to run a query that will return a million or more records.

Try running the following to test this:

SELECT locTime, Count(*) FROM #temp5 GROUP BY locTime HAVING Count(*) > 1 ORDER BY Count(*) DESC

SELECT loc1Time, Count(*) FROM #temp1 GROUP BY loc1Time HAVING Count(*) > 1 ORDER BY Count(*) DESC

etc...

Let me know what you find out.

|||

Well, I went ahead and tested this locally. For my test, I was able to join the tables, each with only 3 duplicates, to return 74,500 rows in less than a second. Below is all of the SQL I used to test this. If you are still seeing the performance problems, it makes me think either the server is way overloaded or there is some other resource constraint impeding your efforts.

Test Table Creation:

ifexists (select *from dbo.sysobjectswhere id =object_id(N'[dbo].[T5]')andOBJECTPROPERTY(id, N'IsUserTable') = 1)drop table [dbo].[T5]GOifexists (select *from dbo.sysobjectswhere id =object_id(N'[dbo].[T1]')andOBJECTPROPERTY(id, N'IsUserTable') = 1)drop table [dbo].[T1]GOifexists (select *from dbo.sysobjectswhere id =object_id(N'[dbo].[T2]')andOBJECTPROPERTY(id, N'IsUserTable') = 1)drop table [dbo].[T2]GOifexists (select *from dbo.sysobjectswhere id =object_id(N'[dbo].[T3]')andOBJECTPROPERTY(id, N'IsUserTable') = 1)drop table [dbo].[T3]GOifexists (select *from dbo.sysobjectswhere id =object_id(N'[dbo].[T4]')andOBJECTPROPERTY(id, N'IsUserTable') = 1)drop table [dbo].[T4]GOCREATE TABLE [dbo].[T5] ([ID] [int]IDENTITY (1, 1)NOT NULL ,[locTime] [datetime]NOT NULL )ON [PRIMARY]GOCREATE TABLE [dbo].[T1] ([ID] [int]IDENTITY (1, 1)NOT NULL ,[loc1Time] [datetime]NOT NULL ,[engValue] [float]NOT NULL )ON [PRIMARY]GOCREATE TABLE [dbo].[T2] ([ID] [int]IDENTITY (1, 1)NOT NULL ,[loc1Time] [datetime]NOT NULL ,[engValue] [float]NOT NULL )ON [PRIMARY]GOCREATE TABLE [dbo].[T3] ([ID] [int]IDENTITY (1, 1)NOT NULL ,[loc1Time] [datetime]NOT NULL ,[engValue] [float]NOT NULL )ON [PRIMARY]GOCREATE TABLE [dbo].[T4] ([ID] [int]IDENTITY (1, 1)NOT NULL ,[loc1Time] [datetime]NOT NULL ,[engValue] [float]NOT NULL )ON [PRIMARY]GOALTER TABLE [dbo].[T5]WITH NOCHECK ADD CONSTRAINT [PK_T5]PRIMARY KEY CLUSTERED ([ID])ON [PRIMARY] GOALTER TABLE [dbo].[T1]WITH NOCHECK ADD CONSTRAINT [PK_T1]PRIMARY KEY CLUSTERED ([ID])ON [PRIMARY] GOALTER TABLE [dbo].[T2]WITH NOCHECK ADD CONSTRAINT [PK_T2]PRIMARY KEY CLUSTERED ([ID])ON [PRIMARY] GOALTER TABLE [dbo].[T3]WITH NOCHECK ADD CONSTRAINT [PK_T3]PRIMARY KEY CLUSTERED ([ID])ON [PRIMARY] GOALTER TABLE [dbo].[T4]WITH NOCHECK ADD CONSTRAINT [PK_T4]PRIMARY KEY CLUSTERED ([ID])ON [PRIMARY] GO

Test Table Population:

DECLARE @.Counterint, @.Datedatetime, @.Table intSET @.Counter = 0SET @.Date ='01/01/2001 00:00'WHILE (@.Counter < 1000)BEGIN IF @.Counter = 350or @.Counter = 700BEGIN SET @.Date ='01/01/2001 00:00'END INSERT T5 (locTime)VALUES (@.Date)SET @.Counter = @.Counter + 1SET @.Date =DateAdd(hh, 1, @.Date)ENDSET @.Table = 1WHILE (@.Table < 5)BEGIN SET @.Counter = 0SET @.Date ='01/01/2001 00:00'WHILE (@.Counter < 1000)BEGIN IF @.Counter = 350or @.Counter = 700BEGIN SET @.Date ='01/01/2001 00:00'END IF @.Table = 1INSERT T1 (loc1Time, engValue)VALUES (@.Date, rand())ELSE IF @.Table = 2INSERT T2 (loc1Time, engValue)VALUES (@.Date, rand())ELSE IF @.Table = 3INSERT T3 (loc1Time, engValue)VALUES (@.Date, rand())ELSE IF @.Table = 4INSERT T4 (loc1Time, engValue)VALUES (@.Date, rand())SET @.Counter = @.Counter + 1SET @.Date =DateAdd(hh, 1, @.Date)END SET @.Table = @.Table + 1ENDselectcount(*)from T5selectcount(*)from T1selectcount(*)from T2selectcount(*)from T3selectcount(*)from T4

Temp Table Join Test:

SELECT *INTO #temp5FROM T5SELECT *INTO #temp1FROM T1SELECT *INTO #temp2FROM T2SELECT *INTO #temp3FROM T3SELECT *INTO #temp4FROM T4Select t1.engValue+t2.engValue+t3.engValue+t4.engValuefrom #temp5 t5innerjoin #temp1 t1on t1.Loc1Time = t5.locTimeinnerjoin #temp2 t2on t2.Loc1Time = t5.locTimeinnerjoin #temp3 t3on t3.Loc1Time = t5.locTimeinnerjoin #temp4 t4on t4.Loc1Time = t5.locTimeDROP TABLE #temp5DROP TABLE #temp1DROP TABLE #temp2DROP TABLE #temp3DROP TABLE #temp4
|||

Hi ZLA

Yes, that can be a possiblity because for certain locValues, I have been having probably 20-30 duplicate locId's that must be the cause of keeping the process busy.

I still need to go over the table example you showed me, I will go over it tomorrow, its been very busy at work today... I am really thankful to you for this help.

Tomorrow I will go over it and will create more duplicate records and will confirm if thats the reason of my problem.

Thanks once again

-Sarah

|||

That sounds like it's the cause, then. If you have trouble figuring out how to handle the duplicates, let me know. You'll need to decide / know what the correct answer should be. For example, if

Temp1.loc1Time = '7/1/2006 12:34', Temp1.engValue = 1.0

Temp1.loc1Time = '7/1/2006 12:34', Temp1.engValue = 2.0

Temp2.loc1Time = '7/1/2006 12:34', Temp2.engValue = 0.1

Temp2.loc1Time = '7/1/2006 12:34', Temp2.engValue = 0.2

Assume all other matching records have engValue of 0.0. Using joins, you would get 4 matches: 1.0 and 0.1, 1.0 and 0.2, 2.0 and 0.1, 2.0 and 0.2. That would give you 4 separate sums for that time: 1.1, 1.2, 2.1 and 2.2. If those four are summed together, the grand total is 6.6.

If, however, the grand total for that time should just be 3.3, then the joins are not the right method.

Let me know what you find out.

|||

Hi ZLA

Yes, thats exactly whats happening, one table has 100 adn another has 98 rows with loc1time as 1/1/1900. and with teh join, when I stop in a matter of seconds its showing 14,000 rows and its still not complete.

Yes, it seems like its giving the result of 6.6 and we want 3.3. Either we need to group by the loc1time in each table adn make sure that there is one record in each of the temp tables or delete teh enteries from teh temp tables that have duplicate joining condition.

I think we are going towards your first solution, which asked for grouping in the fifth table with the expression and summing the values up.

what do you suggest?

-Sarah

|||

Hurrah! I can think of several approaches:

1. Is the 1/1/1900 date valid? If not, either correct the underlying problem or exclude these records from the query. To exclude them, you could eliminate them when creating #temp1, #temp2, ... Or you could add a general where clause to the multiple inner join such as "WHERE temp1.loc1Time <> '1/1/1900' AND temp2loc1Time <> '1/1/1900' ..." Or you could modify the joins to do the filtering: "FROM #temp5 t5 INNER JOIN #temp1 t1 ON t1.Loc1Time = t5.locTime AND t1.Loc1Time <> '1/1/1900' ..." I think the first is best if possible; the last is next best.

2. If duplicate values are not correct, I would recommend building the temp tables so they only hold the distinct values you want. That should be the most efficient. If possible, you could get that by adding DISTINCT to the select statement that builds them. If you can't alter the temp table creation, you can use sub-queries in your join:

Select t1.engValue+t2.engValue+t3.engValue+t4.engValue
from (select distinct locTime from #temp5) t5
innerjoin (select distinct Loc1Time from #temp1) t1
on t1.Loc1Time = t5.locTime
innerjoin (select distinct Loc1Time from #temp2) t2
on t2.Loc1Time = t5.locTime
innerjoin (select distinct Loc1Time from #temp3) t3
on t3.Loc1Time = t5.locTime
innerjoin (select distinct Loc1Time from #temp4) t4
on t4.Loc1Time = t5.locTime

3. Use a summing technique on a separate temp table as I described earlier:

There are other alternatives as well; you might try thinking laterally. Perhaps you can update temp table 5 (or use a separate temp table 6) where you insert 1 row from each of the other 4 temp tables. Just insert locTime, engValue. Then do the following: "SELECT locTime, Sum(engValue) From #Temp6 GROUP BY locTime HAVING Count(*) = 4". Please note that the count assumes only 1 row [datetime] is inserted from each temp table. If not, you will need to alter this approach.

The summing technique would be best if a temp table may not have a datetime that is in one of the other temp tables. That would be dependent on your process. If you only want sums where the datetime is in all 5 tables, then I would use a join. If you want all datetimes then I would look at a summing technique.

HTH. Let me know how it turns out.

How to import tables from other database

Hi there,

I am having difficulties in importing table from one sql server database file to another sql server database file.

A few months ago, I converted access file to ms sql express file. I had made many changes on the ms sql express file, however, the data in this file isn't the latest as the old system is still being used. Now, I want to deploy my new system, I need to import in necessary tables from the old system database, as well as, I want to retain the tables and data I created on the ms sql express file that I have been using so far for development and testing.

May I know how to import tables from other database? Just as in ms access where we can import tables from other access file. I'm using sql express 2005 and sql server management tool. Any advice/help is very much appreciated.

Thanks...

import you Access data to another database on the server and next write T-SQL statement which will move data from old structure to your new structure. If you have access to full SQL server version you can also try SQL SSIS import package but I think that T-SQL is more flexible in this case.|||

u can use this scripter ... http://www.sqlscripter.com/

using this u can create T-SQL ...

How To Import Records From One SQL Tbl To Another SQL Tbl In Same DB

Hello All,
I need to import records from one SQL table to another SQL table. Both tables are in the same database. Does anyone know how?

Thank you,
TinaWell, you could use DTS, or you could use an INSERT query

INSERT INTO newTable (column1, column2, column3...) SELECT column1, column2, column3... FROM oldTable

Terri|||tmorton,
I tried using DTS and got confused. I got the query to work.

Thank You!
Tina

Monday, March 12, 2012

How to Import Data from AS400 files to Sql 2005 tables?

Hi All,

I want to Import data from AS400 to Sql2005. From the Sql Management studio I invoke the Import Data wizard. For the source I connect to the ISereis system and for the Destination I select the Sql2005 Database , When I go to the next step i.e select source tables I get the error
"An error has occured which the SQL Server Integration Wizard was not prepared to handle
No error message available,result codeBig SmileB_E_CANTCANCEL(0*80040E15).(System.Data)" .After this I am unable to proceed further. I am using client Access tool to connect to ISereis

IBM DB2 UDB for isereis IBMDA400 OLE DB Provider

Regds,

Anu

If this is going to be a regular task, might as well dump your AS400 data in a delimited text file and import from there|||Moving to the SSIS forums

Friday, March 9, 2012

How to Import access table into SQL express 2005 database?

Can anyone help me import a ms access (97) table into a sql 2005 express database? The sql database already contains other tables but I would like to add some of my older access tables to this database without having to re-enter all the data. Please help! Thanks!SQL Server Management Studio for SQL Server 2005 (or Enterprise Manager for 2000) has a lovely Data Transformation Service (DTS) package builder.

If you set up a table in your SQL database you can just map the data from access straight to the data source.

DTS package -> look it up :)|||Do you know if this DTS package is present with management studio express? Because I am running sql 2005 express? I'm not having much luck finding it, sorry I'm pretty new with databases.|||maybe this will help: http://www.microsoft.com/sql/solutions/migration/access/default.mspx|||Thanks ! I will give that a try. :)