Showing posts with label copy. Show all posts
Showing posts with label copy. Show all posts

Sunday, March 25, 2012

CREATE TABLE (starting at row ?)

Essentially what I want to do is...

Copy a table from my main SQL server database to Temp, starting at a particular row. Ex: Only include row 1,000 to 1,999 (end row.)

I've been using DTS Wizard to CREATE TABLE, and it's working fine, but I searched and searched in google and this forum and I can't find how to "start at particular row" when creating table.

Thanks,

Bill

hi Bill,

tables are created "without" rows... tables are defined by attributes implemented as columns..

rows usually are not numbered, so you can not say "start at row 1000 and go on until row 1999"... you can select data and insert it into other tables via the INSERT .. SELECT statement but, for your "requirement", you have to perform sort of paging.. that's to say skip the first "n" rows and proceed with the remaining..

a simple "solution" to get this kind of "paging" can be performed via ROW_NUMBER() new Transact-SQL 2005 function..

you project the underlying table's data adding a monotonically increasing new integer row number value..

if you write

SET NOCOUNT ON; USE tempdb; GO CREATE TABLE dbo.TestTB ( Id int NOT NULL PRIMARY KEY, dataValue varchar(10) NOT NULL ); GO DECLARE @.i int; SET @.i = 1; WHILE @.i <= 1000 BEGIN INSERT INTO dbo.TestTB VALUES ( @.i * 10 , CONVERT(varchar, @.i) + 'abc' ); SET @.i = @.i +1 END; GO WITH CTE AS ( SELECT ROW_NUMBER() OVER( ORDER BY Id ) AS rnum, Id, dataValue FROM dbo.TestTB ) SELECT rnum, Id, dataValue FROM CTE WHERE rnum > 50 AND rnum < 100; GO DROP TABLE dbo.TestTB;

you get all the rows with rnum > 50 and < 100..

and you can even project+insert that result to another destination table like

SET NOCOUNT ON; USE tempdb; GO CREATE TABLE dbo.TestTB ( Id int NOT NULL PRIMARY KEY, dataValue varchar(10) NOT NULL ); CREATE TABLE dbo.TestTB2 ( Id int NOT NULL PRIMARY KEY, dataValue varchar(10) NOT NULL ); GO DECLARE @.i int; SET @.i = 1; WHILE @.i <= 1000 BEGIN INSERT INTO dbo.TestTB VALUES ( @.i * 10 , CONVERT(varchar, @.i) + 'abc' ); SET @.i = @.i +1 END; GO WITH CTE AS (SELECT ROW_NUMBER() OVER( ORDER BY Id ) AS rnum, Id, dataValue FROM dbo.TestTB ) INSERT INTO dbo.TestTB2 SELECT Id, dataValue FROM CTE WHERE rnum > 50 AND rnum < 100; SELECT * FROM dbo.TestTB2; GO DROP TABLE dbo.TestTB, dbo.TestTB2;

regards

|||

Hi Andrea,

I was gone all day, sorry I took so long to respond. I appreciate your reply to my question.

I'm extremely new at this, and this looks like a very long statement. What saying is it's kind of over my head. Would I have to change any other parameters other than the Table names? Please excuse my lack of knowledge.

By the way, I like the name of your company... Insulin Power.

Thanks,

Bill

|||

hi Bill

Car54 wrote:

I'm extremely new at this, and this looks like a very long statement. What saying is it's kind of over my head. Would I have to change any other parameters other than the Table names? Please excuse my lack of knowledge.

as you already have "your own" tables, yes, you have to modify them..

the actual statement you have to modify only is

WITH CTE AS (SELECT ROW_NUMBER() OVER( ORDER BY [Id] ) AS rnum, -- modify the eventual order by column [Id], [dataValue] -- modify the returned columns FROM [dbo].[TestTB] -- modify the original table name ) INSERT INTO [dbo].[TestTB2] -- modify the destination table name SELECT [Id], [dataValue] -- modify the columns (returned by the previous Common Table Expression result) FROM CTE WHERE rnum > 50 AND rnum < 100; -- modify the "range" as required

By the way, I like the name of your company... Insulin Power.

I do just hope you do not suffer the same problem

regards

|||

Hi Andrea, thank you for posting this. I'm really new at this and I don't know where I would put the name of the table I'm copying, and I'm not sure where to put the number of the row to start at. I apologize for my lack of knowledge. Can you post where I need to enter the rows numbers or anything else I might have to do?

By the way, I didn't realize you were a diabetic and that was the reason for using that name for your company. I'm very sorry to hear that, I have friends that are diabetic.

Thanks,

Bill

|||

hi Bill,

Car54 wrote:

Hi Andrea, thank you for posting this. I'm really new at this and I don't know where I would put the name of the table I'm copying, and I'm not sure where to put the number of the row to start at. I apologize for my lack of knowledge. Can you post where I need to enter the rows numbers or anything else I might have to do?

WITH CTE AS (SELECT ROW_NUMBER() OVER( ORDER BY [Id] ) AS rnum, [Id], [dataValue] FROM [dbo].[TestTB] ) INSERT INTO [dbo].[TestTB2] SELECT [Id], [dataValue] FROM CTE WHERE rnum > 50 AND rnum < 100;

[Id] is the column by which you will order the resultset of the CTE you can modify accordingly to your need;

[Id], [dataValue] are the columns you need to select in the CTE to be inserted in the destination table; modify that colum list accordingly to your needs

[dbo].[TestTB] is the original table you need to get data from;

[dbo].[TestTB2] is the destination table;

50 and 100 are the "boundaries" starting from and ending to you like to export..

By the way, I didn't realize you were a diabetic and that was the reason for using that name for your company. I'm very sorry to hear that, I have friends that are diabetic.

fortunately I do have to admit I'm quiet "happy"

regards|||

Thank you Andrea, and I hope you have a great weekend.

Bill

|||

hi Bill,

Car54 wrote:

Thank you Andrea, and I hope you have a great weekend.

Bill

you too

Create table

Can I create table doing a copy of another one who already exist?
thank youYou can use
SELECT * INTO NEWTABLE FROM OLDTABLE
"Alberto" <alberto@.nospam.com> wrote in message
news:%23BBFMmMtDHA.2360@.TK2MSFTNGP10.phx.gbl...
> Can I create table doing a copy of another one who already exist?
> thank you
>|||I don't mean insert data. I want create the new table like the old table.
Thank you.
"»ÆÉ½¹âÃ÷¶¥" <leimin@.fujitsu.sh.cn> escribió en el mensaje
news:OK08etMtDHA.3144@.tk2msftngp13.phx.gbl...
> You can use
> SELECT * INTO NEWTABLE FROM OLDTABLE
>
> "Alberto" <alberto@.nospam.com> wrote in message
> news:%23BBFMmMtDHA.2360@.TK2MSFTNGP10.phx.gbl...
> > Can I create table doing a copy of another one who already exist?
> >
> > thank you
> >
> >
>|||1.YOU CAN TRY:
select * into new table from oldtable where 0=1
2.you try open MSSQLSERVER ENTERPRISE MANAGERMENT
select the oletable mouse right key--generate script--save the
script.then you can run the script in Query Analyzer to create new
table.(just change the table name)
"Alberto" <alberto@.nospam.com> wrote in message
news:%23BBFMmMtDHA.2360@.TK2MSFTNGP10.phx.gbl...
> Can I create table doing a copy of another one who already exist?
> thank you
>|||Actually I'm doing it as you say in the point 2 but in others DBMS like
Informix you can do something like "Create newTable like oldTable".
Thank you.
"»ÆÉ½¹âÃ÷¶¥" <leimin@.fujitsu.sh.cn> escribió en el mensaje
news:uJtW9BNtDHA.3536@.tk2msftngp13.phx.gbl...
> 1.YOU CAN TRY:
> select * into new table from oldtable where 0=1
> 2.you try open MSSQLSERVER ENTERPRISE MANAGERMENT
> select the oletable mouse right key--generate script--save the
> script.then you can run the script in Query Analyzer to create new
> table.(just change the table name)
>
> "Alberto" <alberto@.nospam.com> wrote in message
> news:%23BBFMmMtDHA.2360@.TK2MSFTNGP10.phx.gbl...
> > Can I create table doing a copy of another one who already exist?
> >
> > thank you
> >
> >
>|||Closest to that is SELECT INTO. It will not copy over constraint, indexes etc, though.
--
Tibor Karaszi, SQL Server MVP
Archive at: http://groups.google.com/groups?oi=djq&as_ugroup=microsoft.public.sqlserver
"Alberto" <alberto@.nospam.com> wrote in message news:ea38LFNtDHA.2088@.TK2MSFTNGP09.phx.gbl...
> Actually I'm doing it as you say in the point 2 but in others DBMS like
> Informix you can do something like "Create newTable like oldTable".
> Thank you.
> "»ÆÉ½¹âÃ÷¶¥" <leimin@.fujitsu.sh.cn> escribió en el mensaje
> news:uJtW9BNtDHA.3536@.tk2msftngp13.phx.gbl...
> > 1.YOU CAN TRY:
> > select * into new table from oldtable where 0=1
> >
> > 2.you try open MSSQLSERVER ENTERPRISE MANAGERMENT
> > select the oletable mouse right key--generate script--save the
> > script.then you can run the script in Query Analyzer to create new
> > table.(just change the table name)
> >
> >
> >
> > "Alberto" <alberto@.nospam.com> wrote in message
> > news:%23BBFMmMtDHA.2360@.TK2MSFTNGP10.phx.gbl...
> > > Can I create table doing a copy of another one who already exist?
> > >
> > > thank you
> > >
> > >
> >
> >
>

Create Table

Hi All
how can I Create Table (COPY STRUCTURE TO TableName Only ) From Another
Table In The Same Database
ThanksSELECT *
INTO newTable
FROM oldTable
WHERE 1 = 0;
Better yet, store your CREATE TABLE scripts in source control instead of
relying on this. Because you will not get any indexes, constraints, keys,
identity properties, statistics, extended properties, etc.
A
"TAHA" <TAHA105@.HOTMAIL.COM> wrote in message
news:uUamQAzBGHA.344@.TK2MSFTNGP11.phx.gbl...
> Hi All
> how can I Create Table (COPY STRUCTURE TO TableName Only ) From Another
> Table In The Same Database
> Thanks
>
>|||Thank you Aaron
"Aaron Bertrand [SQL Server MVP]" <ten.xoc@.dnartreb.noraa> wrote in message
news:OuEgpGzBGHA.984@.tk2msftngp13.phx.gbl...
> SELECT *
> INTO newTable
> FROM oldTable
> WHERE 1 = 0;
> Better yet, store your CREATE TABLE scripts in source control instead of
> relying on this. Because you will not get any indexes, constraints, keys,
> identity properties, statistics, extended properties, etc.
> A
>
> "TAHA" <TAHA105@.HOTMAIL.COM> wrote in message
> news:uUamQAzBGHA.344@.TK2MSFTNGP11.phx.gbl...
>

Wednesday, March 21, 2012

Create SQL cluster on 2003

I have 2 2003 servers each running a separate copy of SQL. I have purchased
a external storage Dell Powervault running RAID 5 to serve as the shared disk
space. I would like to create a SQL cluster with these 2 machines. Each SQL
server has databases that will need to be moved to the shared space. What is
the easiest way to accomplish this?
I was thinking I would need to do backup my databases from both SQL servers.
Create a cluster in 2003 cluster management
Uninstall SQL server from both SQL servers
Install SQL server as a virtual server from one of the 2003 servers.
Restore the SQL databases to the shared disk space
Am I missing anything?
First, your configuration is unsupported. A cluster must be purchased as a
cluster, not just assembled ad-hoc from components that may or may not be on
the cluster Hardware Compatibility list in order to be a supported
configuration. Some storage vendors will certify the entire platform if you
purchase installation services along with the storage device.
Second, your RAID-5 Powervault will run very slowly in a cluster. RAID-5
has significant overhead for writes. Normally a caching controller can
mitigate these issues but with clustering, all SCSI controllers for shared
storage must disable write cache. Since you have the PowerVault divided
into a single array, you will have to install SQL onto the Quorum partition,
again an unsupported configuration. Note that Clustering will work at the
RAID container level, not at the logical partition level. Data and
transaction logs will be on the same physical device so there goes another
bit of performance and recoverability. The whole purpose of SQL Clustering
is to increase availability. I don't see how this configuration will help
reach that goal.
I would talk to my Dell representative about their certified cluster
offerings rather than pursue this path.
Since you did ask for how to do something instead of whether it should be
done, here goes. Create a cluster and install an instance of SQL onto the
cluster (likely a named instance since I would guess that the local
machine(s) already use a default instance). After that, it is a simple
matter to move the databases as you would between any two SQL servers.
Windows 2003 Server has a really great clustering wizard that keeps you from
building a non-functional cluster. Once that is working, you can easily
install SQL clustering according to the instructions in BOL.
HOW TO: Move Databases Between Computers That Are Running SQL Server
http://support.microsoft.com/default...b;en-us;314546
Geoff N. Hiten
Microsoft SQL Server MVP
Senior Database Administrator
"Amy Lewis" <AmyLewis@.discussions.microsoft.com> wrote in message
news:EF12ECFD-4BA7-48DD-8605-46D045E39532@.microsoft.com...
>I have 2 2003 servers each running a separate copy of SQL. I have
>purchased
> a external storage Dell Powervault running RAID 5 to serve as the shared
> disk
> space. I would like to create a SQL cluster with these 2 machines. Each
> SQL
> server has databases that will need to be moved to the shared space. What
> is
> the easiest way to accomplish this?
> I was thinking I would need to do backup my databases from both SQL
> servers.
> Create a cluster in 2003 cluster management
> Uninstall SQL server from both SQL servers
> Install SQL server as a virtual server from one of the 2003 servers.
> Restore the SQL databases to the shared disk space
> Am I missing anything?
|||Thanks for the response. I have actually talked with Dell about this and
given the small volume of SQL database activity - they recommended this.
I have not configured my PowerVault yet - would Raid 1 be better relating
to performance? My current 2003 servers have a single RAID 5 configuration -
and the databases are stored in the normal c:\program files\.... and it
seems to be working fine for us. We only about about 20 databases - all
small (the largest is 500M) and all with less than 20 users connected at 1
time.
"Geoff N. Hiten" wrote:

> First, your configuration is unsupported. A cluster must be purchased as a
> cluster, not just assembled ad-hoc from components that may or may not be on
> the cluster Hardware Compatibility list in order to be a supported
> configuration. Some storage vendors will certify the entire platform if you
> purchase installation services along with the storage device.
> Second, your RAID-5 Powervault will run very slowly in a cluster. RAID-5
> has significant overhead for writes. Normally a caching controller can
> mitigate these issues but with clustering, all SCSI controllers for shared
> storage must disable write cache. Since you have the PowerVault divided
> into a single array, you will have to install SQL onto the Quorum partition,
> again an unsupported configuration. Note that Clustering will work at the
> RAID container level, not at the logical partition level. Data and
> transaction logs will be on the same physical device so there goes another
> bit of performance and recoverability. The whole purpose of SQL Clustering
> is to increase availability. I don't see how this configuration will help
> reach that goal.
> I would talk to my Dell representative about their certified cluster
> offerings rather than pursue this path.
> Since you did ask for how to do something instead of whether it should be
> done, here goes. Create a cluster and install an instance of SQL onto the
> cluster (likely a named instance since I would guess that the local
> machine(s) already use a default instance). After that, it is a simple
> matter to move the databases as you would between any two SQL servers.
> Windows 2003 Server has a really great clustering wizard that keeps you from
> building a non-functional cluster. Once that is working, you can easily
> install SQL clustering according to the instructions in BOL.
> HOW TO: Move Databases Between Computers That Are Running SQL Server
> http://support.microsoft.com/default...b;en-us;314546
> Geoff N. Hiten
> Microsoft SQL Server MVP
> Senior Database Administrator
>
> "Amy Lewis" <AmyLewis@.discussions.microsoft.com> wrote in message
> news:EF12ECFD-4BA7-48DD-8605-46D045E39532@.microsoft.com...
>
>
|||I am assuming a PV 220S with 14 slots.
2ea RAID-1 drives for Quorum and MSDTC (36GB 15KRPM) Normal best practices
has them apart but with your small scale combining them should be safe.
2ea RAID-1 drives for Logs (73GB 15KRPM)
2ea RAID-1 drives for Data (146GB 15KRPM)
That leaves 8 slots for future expansion. Make sure you have blanks so the
airflow works correctly. You can adjust the sizes of the drives to meet
your needs, but try to keep the Quorum and Logs drives at 15KRPM. The speed
definitely makes a difference. Since you are in a cluster configuration,
the physical location of the drives in the individual slots makes no
difference. This will give you a decent performing system that is also
pretty reliable and recoverable.
Geoff N. Hiten
Microsoft SQL Server MVP
"Amy Lewis" <AmyLewis@.discussions.microsoft.com> wrote in message
news:F0ECD371-DAB4-433C-8445-750EB2D45AA3@.microsoft.com...[vbcol=seagreen]
> Thanks for the response. I have actually talked with Dell about this and
> given the small volume of SQL database activity - they recommended this.
> I have not configured my PowerVault yet - would Raid 1 be better relating
> to performance? My current 2003 servers have a single RAID 5
> configuration -
> and the databases are stored in the normal c:\program files\.... and it
> seems to be working fine for us. We only about about 20 databases - all
> small (the largest is 500M) and all with less than 20 users connected at 1
> time.
> "Geoff N. Hiten" wrote:

Sunday, February 19, 2012

Create Duplicate SQL Server with Databases

Hi,
Is there a way to copy a SQL Server along with its databases and all other
configuration to other servers? Please keep in mind that all the other
servers are in different Active Directory/domains.
Thank you.Hi,
Thank you for your reply.
Reinstalling SQL is what I would like to avoid. I have to create SQL/Win2K
servers on a regular basis for our clients. I was trying to find a way so I
can simply ghost the server. The base configuration is the same for all our
server so the database do not change until it's been installed. Even then,
only data changes, not the DBs themselves.
Thank you.
"Stressed" <k@.c.co.uk> wrote in message
news:%23I5LeawYDHA.652@.TK2MSFTNGP10.phx.gbl...
> You may be able to install a new copy of sql server, so all the
> settings/registry keys are up to scratch on the new server, and
> then copy everything to the new server, so long as the other server
> has the same configuration and disk letters.
> I've not tried this, though, HTH.
>
> "Dragon" <nopam_baadil@.hotmail.com> wrote in message
> news:eK9VI4oYDHA.2448@.TK2MSFTNGP09.phx.gbl...
> > Hi,
> >
> > Is there a way to copy a SQL Server along with its databases and all
other
> > configuration to other servers? Please keep in mind that all the other
> > servers are in different Active Directory/domains.
> >
> > Thank you.
> >
> >
>

Create Duplicate of table ?

I want to create a copy of a table with a new name, what is the best way to
do this. I did notice I could copy a table, but there is no paste !?!SELECT * INTO CopyOfMyTable FROM MyTable WHERE 1=2
1=2 means that only table structure will be copied without data. The
downside is that if you create a new table that way
SQL Server will not transfer PK and FK as well as Indexes and Constraints
created on original table
An Identity property SQL Server does transfer.
"p" <p@.p.com> wrote in message
news:OKMLB2k%23FHA.532@.TK2MSFTNGP15.phx.gbl...
>I want to create a copy of a table with a new name, what is the best way to
>do this. I did notice I could copy a table, but there is no paste !?!
>|||SELECT * INTO Newtablename
FROm OldTableName
HTH, jens Suessmeyer.|||You guys are realy great for coming back so quick with an answer. Thanks to
both of you for the replies.
"Jens" <Jens@.sqlserver2005.de> wrote in message
news:1133865309.548099.228200@.g43g2000cwa.googlegroups.com...
> SELECT * INTO Newtablename
> FROm OldTableName
> HTH, jens Suessmeyer.
>

Tuesday, February 14, 2012

Create copy of database and place on different server

All,
I am trying to create a copy of an existing database and place on a different sql server for testing purposes. Currently my method of doing this is to create a backup, then restore a database on my new sql server. But here is the problem I am running into. When I look in Enterprise manager I can see all of the stored procedures and tables and data just fine, which is how I would like it. But, when I open up Query Analyzer I am not able to run any queries because it says "invalid object name" error. I know that the object name is correct. I think that I am having a conflict between the users which were carried over from the source backup file and the users that are on my 2 new sql server. If anyone could help, I would really appreciate it. I am stuck at this point. I am guessing it is probably something simple I am just unaware that I need to do.

Thanks,
LinsLoHi,

Maybe you should try a detach

EXEC sp_detach_db 'your_db'

And then on the second server an attach:

EXEC sp_attach_db @.dbname = N'pubs',
@.filename1 = N'c:\Program Files\Microsoft SQL Server\MSSQL\Data\pubs.mdf',
@.filename2 = N'c:\Program Files\Microsoft SQL Server\MSSQL\Data\pubs_log.ldf'|||LinsLo,

The problem is probably being caused because the userids are not matching up in your system tables.

First make sure the usernames for the database exist on the server then for each user run the following sql script.

EXEC sp_change_users_login 'Update_One', '<USERNAME>', '<USERNAME>'

That will align the users in the database with the users on the server.

Hope this helps.|||My first guess would be that you need to use sp_change_users_login (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_sp_ca-cz_8qzy.asp).

-PatP