Showing posts with label updating. Show all posts
Showing posts with label updating. Show all posts

Monday, March 19, 2012

Help with a Query

Is there a way to optimize this query, by doing it in fewer steps?
Perhaps by only utilizing and updating one table?
-TIA-
code:

/ ****************************************
****
Import the data from DBCC SQLPERF(LOGSPACE)
****************************************
****/
CREATE TABLE #dbcc_sqlperf (
DB_Name varchar(100),
Log_Size decimal (28, 5),
Log_Used_Percent decimal (28, 5),
Status tinyint )
INSERT #dbcc_sqlperf EXEC ('DBCC SQLPERF(LOGSPACE) WITH NO_INFOMSGS')
SELECT * FROM #dbcc_sqlperf --Debug
/ ****************************************
****
Convert the data form DBCC SQLPERF(LOGSPACE)
into a more legible form
****************************************
****/
CREATE TABLE #logstats (
DB_ID tinyint,
DB_Name varchar(100),
Logical_Name varchar(100),
File_Type varchar(10),
Total_Space_in_MB decimal (28, 2),
Used_Space_in_MB decimal (28, 2),
Free_Space_in_MB decimal (28, 2),
Percent_Used decimal (28, 2))
INSERT #logstats (DB_Name, File_Type, Total_Space_in_MB,
Used_Space_in_MB,
Free_Space_in_MB, Percent_Used)
SELECT DB_Name = DB_Name,
File_Type = 'Log',
Total_Space_in_MB = log_size,
Used_Space_in_MB = sum (log_size*(log_used_percent/100)),
Free_Space_in_MB = sum (log_size
-(log_size*(log_used_percent/100))),
Percent_Used = log_used_percent
FROM #dbcc_sqlperf
GROUP BY DB_Name, Log_Size, Log_Used_Percent, Status
/ ****************************************
****
Collect the DBID number for each log file
****************************************
****/
UPDATE l
SET DB_ID = m.dbid
FROM #logstats l INNER JOIN master..sysdatabases m
ON m.name = l.DB_Name
/ ****************************************
****
colloct the Logical Name for each log file
****************************************
****/
UPDATE l
SET Logical_Name = m.name
FROM #logstats l INNER JOIN master..sysaltfiles m
ON m.dbid = l.DB_ID AND m.groupid = '0'
/ ****************************************
****
Display and drop temp tables
****************************************
****/
SELECT * FROM #logstats
DROP TABLE #logstats
DROP TABLE #dbcc_sqlperf

What exactly do you expect this script to do? What are you trying to
accomplish?
Anith|||This is a small snippet of a larger project that you can find at
http://groups.google.com/group/micr...939daf22f9796b8
Anyway the idea behind this is to continually (every 15 minutes or so)
go out to all the servers and collect the statistics of the databases.
This will then be stored in a centralized location where we can tract
trends, and or raise alarms if some of our developers do a bulk insert
(with out tell us of course). I just would like to do it the most
efficient way the first time. If I can reduce the I/O's and CPU by
even a couple of points then its worth it.
I think I tend to scare everyone off when I post a huge chunk of code,
so i am doing it piecemeal
Thanks
-Matt-|||>> I think I tend to scare everyone off when I post a huge chunk of code,
Of course you are :-)
Try creating a script & schedule it to run every 15 minutes and insert the
results into a permanent base table. Here is a simple re-write without
changing much of what you posted:
CREATE TABLE #dbcc_sqlperf (
database_name VARCHAR(40), Log_Size DECIMAL (28, 5),
Log_Used_Percent DECIMAL (28, 5), Status TINYINT );
INSERT #dbcc_sqlperf EXEC ('DBCC SQLPERF(LOGSPACE) WITH NO_INFOMSGS')
SELECT DB_ID( database_name ) AS "database_id",
database_name AS "database_name",
( SELECT m.name
FROM master..sysaltfiles m
WHERE m.dbid = DB_ID(l.database_name)
AND m.groupid = '0' ) AS "logical file name",
'Log' AS "file_type",
log_size AS "total space in MB",
SUM (log_size * log_used_percent * .001) AS "used space in MB",
SUM (log_size - (log_size * log_used_percent * .001)) AS "free space
in MB",
log_used_percent AS "percent used"
FROM #dbcc_sqlperf l
GROUP BY database_name, Log_Size, Log_Used_Percent, Status ;
You can use a table variable to avoid temp table usage. Alternatively, you
may be able to retrieve similar information using the system procedure
sp_spaceused as well.
Anith|||Thanks Anith,
I really appreciate this. I am always looking to speed up and
bulletproof my code. if i can save a few extra cycles, during the
lifetime code that will run, could be several hours. Sure this might be
trivial, but if I reuse this code somewhere I know that I have one of
the most efficient pieces premade.

Wednesday, March 7, 2012

Help Updating Tables From TAB File

Hi All

Im Really New To SQL Server and need to do the folowing - if anyone can help out i will be VERY happy :)

i have a single table in sql server containing products im selling

withing that table is 2 fields that i need to be able to update without changing any of the other fields in that line. (i need to be able to update price and stock field)

there is a field that is unique (field with the barcode)

the data i need to import is from a TAB text file which contains the barcode, stock and price field

hope this makes sense and if anyone can help thats great

cheers

BenHowdy

You can use DTS to import data which will suck data in from the tab file. However, unless you have a bit of experience with SQL its probably worth trying this on some dummy tables to get it right.
You could use DTS to import the tab data into a new table. Then write some code to work your way through the new table to update the existing production table.

If you wanted to update a column based on a unique column value in a table you could use:

Update <tabel_name>
set <column_to_be_updated> = '<some_value>'
where <unique_column> = '<some_value>'

i.e.

update table_product_data
set saleprice = '100'
where barcode = '097364543'

This ensures only the saleprice column in the table is changed where the barcode column = some unique value. That way you can selectively target a row in a particular column based on a value in the barcode column on the same row. Make sure that the barcode column ( or which ever column you use as the primary key for the table ) has unique values, otherwise you may get multiple rows in the saleprice column being updated at the same time.

Post back if probs.

Cheers,

SG.|||thanks for the reply

the problem is i am cool getting it to update 1 record its getting it to run thro a massive tab file and updating about 1500 prices

any suggestions for a thick person ?

cheers

ben|||right i have realised how to do this now - if anyone can give me some example code for in sql to loop thro i have made it import into a new table and i under stand the code you put before but im not sure how to make it loop thro

hope that makes sense!

cheers

ben|||Hi there!

Here I can give you a little example code. Let's say that:
1. "original" - is the table in the database
2. "new" - is the table which has been filled with the data out of the TAB file

update original
set original.stock = new.stock,
original.price = new.price
from original
,new
where original.bar_code_id = new.bar_code_id

Hope that helps you further. If not, post a reply!

Greetings,
Carsten

NOTE: This will not bring any new bar_code_id into "original"! It just updates existing id's|||thats great thanks

will that loop thro each result in the table new ?

thanks for the help

Ben|||you both rule!

its all working now:D:D - you cant believe how happy i am

please both check you private messages

thansk ben

Help Updating Data Across Servers

Hi,
We have two DataBases Residing on two different Servers. We would like to
set up real time uodates between these two servers.
We have an Inventory System on server A and a Tracking System on Server B.
Now an Transaction in Server B should Update a Certain Table or Tables on
Server A. and vice versa.
I would like people to help me choose the Best soulution for this to work
successfully.
I was leaning towards Linked Servers anfd The making use of triggers. Is
this a Good Option.
But waht happens if the Trigger Fails how do we get the data Across to the
Servers.
Thanks for your help in advance.
Thanks,
Jothi
Hi,
If the database is residing in 2 servers and if you need to update data to
and fro then the only solution is using Linked servers and Triggers
But update Trigger using linked server will slow down the process.
Thanks
Hari
SQL Server MVP
"Jothi" <Jothi@.discussions.microsoft.com> wrote in message
news:1B9DD1BE-0DFB-4480-BD24-97C4C10A97C2@.microsoft.com...
> Hi,
> We have two DataBases Residing on two different Servers. We would like to
> set up real time uodates between these two servers.
> We have an Inventory System on server A and a Tracking System on Server B.
> Now an Transaction in Server B should Update a Certain Table or Tables on
> Server A. and vice versa.
> I would like people to help me choose the Best soulution for this to work
> successfully.
> I was leaning towards Linked Servers anfd The making use of triggers. Is
> this a Good Option.
> But waht happens if the Trigger Fails how do we get the data Across to the
> Servers.
> Thanks for your help in advance.
> Thanks,
> Jothi
>
|||hari,
Yes the data is acroos two servers.
How do i handle situations where a trigger fails.
DUe to non avialability of a server.
Thanks,
Jothi
"Hari Prasad" wrote:

> Hi,
> If the database is residing in 2 servers and if you need to update data to
> and fro then the only solution is using Linked servers and Triggers
> But update Trigger using linked server will slow down the process.
> Thanks
> Hari
> SQL Server MVP
> "Jothi" <Jothi@.discussions.microsoft.com> wrote in message
> news:1B9DD1BE-0DFB-4480-BD24-97C4C10A97C2@.microsoft.com...
>
>

Help Updating Data Across Servers

Hi,
We have two DataBases Residing on two different Servers. We would like to
set up real time uodates between these two servers.
We have an Inventory System on server A and a Tracking System on Server B.
Now an Transaction in Server B should Update a Certain Table or Tables on
Server A. and vice versa.
I would like people to help me choose the Best soulution for this to work
successfully.
I was leaning towards Linked Servers anfd The making use of triggers. Is
this a Good Option.
But waht happens if the Trigger Fails how do we get the data Across to the
Servers.
Thanks for your help in advance.
Thanks,
JothiHi,
If the database is residing in 2 servers and if you need to update data to
and fro then the only solution is using Linked servers and Triggers
But update Trigger using linked server will slow down the process.
Thanks
Hari
SQL Server MVP
"Jothi" <Jothi@.discussions.microsoft.com> wrote in message
news:1B9DD1BE-0DFB-4480-BD24-97C4C10A97C2@.microsoft.com...
> Hi,
> We have two DataBases Residing on two different Servers. We would like to
> set up real time uodates between these two servers.
> We have an Inventory System on server A and a Tracking System on Server B.
> Now an Transaction in Server B should Update a Certain Table or Tables on
> Server A. and vice versa.
> I would like people to help me choose the Best soulution for this to work
> successfully.
> I was leaning towards Linked Servers anfd The making use of triggers. Is
> this a Good Option.
> But waht happens if the Trigger Fails how do we get the data Across to the
> Servers.
> Thanks for your help in advance.
> Thanks,
> Jothi
>|||hari,
Yes the data is acroos two servers.
How do i handle situations where a trigger fails.
DUe to non avialability of a server.
Thanks,
Jothi
"Hari Prasad" wrote:

> Hi,
> If the database is residing in 2 servers and if you need to update data to
> and fro then the only solution is using Linked servers and Triggers
> But update Trigger using linked server will slow down the process.
> Thanks
> Hari
> SQL Server MVP
> "Jothi" <Jothi@.discussions.microsoft.com> wrote in message
> news:1B9DD1BE-0DFB-4480-BD24-97C4C10A97C2@.microsoft.com...
>
>

Help Updating Data Across Servers

Hi,
We have two DataBases Residing on two different Servers. We would like to
set up real time uodates between these two servers.
We have an Inventory System on server A and a Tracking System on Server B.
Now an Transaction in Server B should Update a Certain Table or Tables on
Server A. and vice versa.
I would like people to help me choose the Best soulution for this to work
successfully.
I was leaning towards Linked Servers anfd The making use of triggers. Is
this a Good Option.
But waht happens if the Trigger Fails how do we get the data Across to the
Servers.
Thanks for your help in advance.
Thanks,
JothiHi,
If the database is residing in 2 servers and if you need to update data to
and fro then the only solution is using Linked servers and Triggers:)
But update Trigger using linked server will slow down the process.
Thanks
Hari
SQL Server MVP
"Jothi" <Jothi@.discussions.microsoft.com> wrote in message
news:1B9DD1BE-0DFB-4480-BD24-97C4C10A97C2@.microsoft.com...
> Hi,
> We have two DataBases Residing on two different Servers. We would like to
> set up real time uodates between these two servers.
> We have an Inventory System on server A and a Tracking System on Server B.
> Now an Transaction in Server B should Update a Certain Table or Tables on
> Server A. and vice versa.
> I would like people to help me choose the Best soulution for this to work
> successfully.
> I was leaning towards Linked Servers anfd The making use of triggers. Is
> this a Good Option.
> But waht happens if the Trigger Fails how do we get the data Across to the
> Servers.
> Thanks for your help in advance.
> Thanks,
> Jothi
>|||hari,
Yes the data is acroos two servers.
How do i handle situations where a trigger fails.
DUe to non avialability of a server.
Thanks,
Jothi
"Hari Prasad" wrote:
> Hi,
> If the database is residing in 2 servers and if you need to update data to
> and fro then the only solution is using Linked servers and Triggers:)
> But update Trigger using linked server will slow down the process.
> Thanks
> Hari
> SQL Server MVP
> "Jothi" <Jothi@.discussions.microsoft.com> wrote in message
> news:1B9DD1BE-0DFB-4480-BD24-97C4C10A97C2@.microsoft.com...
> > Hi,
> > We have two DataBases Residing on two different Servers. We would like to
> > set up real time uodates between these two servers.
> > We have an Inventory System on server A and a Tracking System on Server B.
> > Now an Transaction in Server B should Update a Certain Table or Tables on
> > Server A. and vice versa.
> >
> > I would like people to help me choose the Best soulution for this to work
> > successfully.
> >
> > I was leaning towards Linked Servers anfd The making use of triggers. Is
> > this a Good Option.
> >
> > But waht happens if the Trigger Fails how do we get the data Across to the
> > Servers.
> >
> > Thanks for your help in advance.
> >
> > Thanks,
> > Jothi
> >
>
>