Showing posts with label existing. Show all posts
Showing posts with label existing. Show all posts

Friday, March 30, 2012

Replacing existing records in a table

I have a table that stores all the processed data from other tables. How can I replace the same data in this table when I do "reprocessing"? It's kinda like a combination of delete then insert kind of thing. I cannot simply insert as it will become duplicates. any idea?updates?

The message I have entered is too short.|||can UPDATE do multiple column update?

actually the purpose is for rerun of query. If i were to use UPDATE, i need to check if the record exist first. That's not efficient. I am looking for direct replacement.|||can UPDATE do multiple column update?

yes

actually the purpose is for rerun of query. If i were to use UPDATE, i need to check if the record exist first.

maybe. maybe not.

That's not efficient. I am looking for direct replacement.

really? that is all in how you skin the cat.

why don't you read Brett Kaiser's sticky at the top of this board, follow the directions, and save everyone a lot of time.

Wednesday, March 28, 2012

Replacing a column with a foreign key

In an *existing* database, how would you remove the City column in theEmployees table, and put in the CityID key column from the Cities table?first you would add a cityid column with alter table add column syntax.
Then to add the data, you could do a join with cities table.

UPDATE
Employees
SET
Cityid = C.CityID
FROM
Employees E
INNER JOIN Cities C
ON E.city = C.city

Then, removing the city column should be as simple as alter table remove column.|||

The following simplified version also works:

UPDATE Employees
SET Cityid = C.CityID
FROM Cities C
WHERE Employees.City = C.City

Wednesday, March 21, 2012

Replace existing table

Hi. I am new to SQL Server so please excuse me if my question is stupid or asked in the wrong place.

I am replicating a number of Visual FoxPro tables in SQL Server. The tables are updated daily. I have got several of them replicated fine through using BULK INSERT to load the existing data and INSERT/UPDATE/DELETE to replicate the daily updates.

However, one of the Visual FoxPro tables is re-created from scratch each day. I could do the same in SQL Server but I would like users to have uninterrupted access to it (i.e. I do not want a query to fail while I am deleting and then re-upsizing the table).

One method that occurs to me would be to upsize the table under a different temporary name and then BEGIN TRANSACTION, delete the old table, rename the new table to the old table and then COMMIT TRANSACTION. Would this work? And is there a better way of doing this?

Many thanks

Dom

I know the users have to go to lunch daily..., so announce a lunch break at 1 pm for all the people and do what to do; if your users don't want to leave do it after they go home.

Sure ,before take a full backup if you have time.

|||Thanks ggciubuc, but I'm afraid thats not an option. The update needs to be done as early as possible (but no earlier than 8am) so it cannot wait until the users have gone home, but it will not be at a consistent time of day because the start time depends on the time taken to complete a number of prior processes.|||

Make a Integration Services package , put it in a job that runs before 8 a.m (eg. 6 a.m); the package can contain:

1.a task that delete the SQL table

2. a task that re-create the SQL table

3. a task with bulk insert

with workflow you can prioritize the processes|||

Thanks Gigi - I have not come across Integration Services before, I will check it out.

However, as stated above, it cannot run earlier than 8am (typically it will run at some point around 10am but some days it may not be until 1pm or later) and I would like to avoid disrupting users if possible.

|||

I recommend to use your proposed solution (with minor change) for this task & (it may be done on the SSIS Package or DTS package), but your approach seems to be good enough to fit for your requirement.

1. Create a table on different temp name (not temp table -- #)

2. Do the manipulation on created table

3. On single transaction, Delete the existing row, insert from created table, drop the created table.

|||

Many thanks Manivannan, I'll give it a go. Deleting all of the the existing rows and then inserting the new records makes much more sense, I guess.

Could I even use a BULK INSERT to append the records (does BULK INSERT work inside a transaction?) and avoid creating the temporary table altogether?

|||

You can use the BULK INSERT on explicit(user) transaction. It can be roll backed also. J

Tuesday, March 20, 2012

replace

Am pretty new to SQL Server..
Am exploring the existing SPs..
I would like to know what the command replace does?
Also what does the convert do??
TIA,
Nishasee REPLACE (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_ca-co_2f3o.asp) and CAST and CONVERT (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_ca-co_2f3o.asp) in the microsoft sql/server web site

rudy
http://rudy.ca/|||also, the Books OnLine shipped with SQL Server is an excelent source of information.

Half or more of all questions asked on this board could have been asnwered by reading this document first.

Saturday, February 25, 2012

reorg database files

I want to reorganize the data files for optimum
performance. I have described below the existing
and the intended scenario that I wish to attain.
** existing database scenario **
database files
mydb.mdf (50gb) - primary data file
mydblog.ldf (1gb) - log file
** intended database scenario **
database files:
mydb1.mdf (10gb) - primary data file
mydb2.ndf (10gb) - secondary data file
mydb3.ndf (10gb) - secondary data file
mydb4.ndf (10gb) - secondary data file
mydb5.ndf (10gb) - secondary data file
mydblog.ldf (1gb) - log file
Are there tools that can me help do this?
Thank you in advance.Using T-SQL tools, you can do this:
Use ALTER DATABASE to create new files and place them in filegroups.
Now, use sp_spaceused to determine which tables and/or indexes you want to
move to the new filegroups.
You can use ALTER TABLE with ON FileGroupName to move data around. Notice
that the filegroup is actually home to an index, but a clustered index is
the table, of course. From the BOL:
ON {filegroup | DEFAULT}
Specifies the storage location of the index created for the constraint. If
filegroup is specified, the index is created in the named filegroup. If
DEFAULT is specified, the index is created in the default filegroup. If ON
is not specified, the index is created in the filegroup that contains the
table. If ON is specified when adding a clustered index for a PRIMARY KEY or
UNIQUE constraint, the entire table is moved to the specified filegroup when
the clustered index is created.
Once you have successfully moved tables by recreating the clustered indexes,
you can use DBCC SHRINKFILE to shrink the original large file down to the
appropriate size.
Russell Fields
"fragb" <anonymous@.discussions.microsoft.com> wrote in message
news:0ca401c47b0c$fb6e7280$a401280a@.phx.gbl...
> I want to reorganize the data files for optimum
> performance. I have described below the existing
> and the intended scenario that I wish to attain.
> ** existing database scenario **
> database files
> mydb.mdf (50gb) - primary data file
> mydblog.ldf (1gb) - log file
> ** intended database scenario **
> database files:
> mydb1.mdf (10gb) - primary data file
> mydb2.ndf (10gb) - secondary data file
> mydb3.ndf (10gb) - secondary data file
> mydb4.ndf (10gb) - secondary data file
> mydb5.ndf (10gb) - secondary data file
> mydblog.ldf (1gb) - log file
> Are there tools that can me help do this?
> Thank you in advance.
>|||None that I know of that would do much in that situation. If you want to
spread your data evenly across a filegroup with multiple files from one that
has a single file you pretty much have to export all the data. Then
truncate all the tables and reimport it back again. It's not that difficult
of a task but obviously you will need to take your users off line for some
period of time. In the past when I have done this I basically scripted out
the database and all the objects in such a way that I could recreate the
database schema with the new files and all the tables , sp's UDF's ect but
leaving off the triggers, RI and Indexes. Then after you BCP out all the
data you can drop the DB and recreate it without those to make it easier and
faster to load. Then Bulk Insert the data and add back the RI, Triggers
etc. Just make sure you have good and tested backups first.
--
Andrew J. Kelly SQL MVP
"fragb" <anonymous@.discussions.microsoft.com> wrote in message
news:0ca401c47b0c$fb6e7280$a401280a@.phx.gbl...
> I want to reorganize the data files for optimum
> performance. I have described below the existing
> and the intended scenario that I wish to attain.
> ** existing database scenario **
> database files
> mydb.mdf (50gb) - primary data file
> mydblog.ldf (1gb) - log file
> ** intended database scenario **
> database files:
> mydb1.mdf (10gb) - primary data file
> mydb2.ndf (10gb) - secondary data file
> mydb3.ndf (10gb) - secondary data file
> mydb4.ndf (10gb) - secondary data file
> mydb5.ndf (10gb) - secondary data file
> mydblog.ldf (1gb) - log file
> Are there tools that can me help do this?
> Thank you in advance.
>|||Hi,
The CREATE TABLE & ALTER TABLE statements only allow you to specify the
FILEGROUP on which you wish to create the table. Not the actual data
file within the Filegroup.
In your intended scenario, you do not draw a distinction between Data
Files, and FileGroups. A possible alternative is :
FileGroupPRIMARY mydb1.mdf (10gb)
FileGroup02 mydb2.ndf (10gb)
FileGroup03 mydb3.ndf (10gb)
FileGroup04 mydb4.ndf (10gb)
FileGroup05 mydb5.ndf (10gb)
mydblog.ldf (1gb)
I use Power Designer - Data Architect to model my databases. After I
make changes to the model (ie, change the FileGroup for a table), Data
Architect compares my Model against the database on the server, and
generates a "Modify" script, which I then run against the database.
It generates the typical script to move to a different Filegroup :
alter table dbo.tbl_Customer
drop constraint PK_Customer
go
alter table dbo.tbl_Customer
add constraint PK_Customer primary key clustered (CustomerId)
on "NEW_FILEGROUP"
go
thanks
Ian
fragb wrote:
> I want to reorganize the data files for optimum
> performance. I have described below the existing
> and the intended scenario that I wish to attain.
> ** existing database scenario **
> database files
> mydb.mdf (50gb) - primary data file
> mydblog.ldf (1gb) - log file
> ** intended database scenario **
> database files:
> mydb1.mdf (10gb) - primary data file
> mydb2.ndf (10gb) - secondary data file
> mydb3.ndf (10gb) - secondary data file
> mydb4.ndf (10gb) - secondary data file
> mydb5.ndf (10gb) - secondary data file
> mydblog.ldf (1gb) - log file
> Are there tools that can me help do this?
> Thank you in advance.
>