Showing posts with label duplicate. Show all posts
Showing posts with label duplicate. Show all posts

Friday, March 30, 2012

How to exclude duplicate records from totals

My column figures are correct in my report, but duplicate values are being added to the totals.

I am using:

Format(Sum(Fields!ACEG_Contribution.Value), "C")

This is not a matrix report. I am using tables so it only has table headers and table footers.

How can I fix this? Please advise a-sap.

Thanx in advance for any assistance you can provide,

gb

Hi Gerry-

Not completly certain where your duplication is coming from - as per your description. If duplicate values are present in your data, you would want to use the DISTINCT clause in your query to filter duplicates.

If you have groupings in your table, and want to subtotal rather than grandtotal, you can use the scope argument on the SUM function to filter per group i.e. Sum(Fields!Value, "Group1)".

If you want to display duplicates, bu only sum the non-duplicates, you would need to have a separate query which uses the DISTINCT caluse, then create an expression in your table footer which does sum of second dataset. i.e. SUM(Fields!Value, "DataSet2")

Hope that helps,

Thanks, Jon

Wednesday, March 21, 2012

How to eliminate duplicate data

I have a table with 68 columns. If all the columns hold the same value except for one which is a datetime column I want to delete all but one of the duplicate rows. Preferably the latest one but that is not important. Can someone show me how to accomplish this?

You can use the Group By fucntion

SELECT col_a, col_b, col_c From Table GROUP BY col_a, col_b, col_c

If you want the newest date - you can also using HAVING Clause

SELECT col_a, col_b, col_c From Table GROUP BY col_a, col_b, col_c HAVING max(col_date)

WHERE col_a - col_c are the columns with the same data and col_date is your date column

AWAL

|||Is this a easier process if I manually delete them? I just want to view the duplicate data but my problem is that the DateTime column in my table is unique unlike all the other columns with the same values.|||

John -

I'm not quite certain what you mean but if your datetime column is not unique but you want dups of all the other columns just don't group by the datetime column.

AWAL

|||

Assuming that your data isn't too big, you can use a technique like this:

drop table removeDups
go
create table removeDups(
column1 int,
column2 int,
column3 int,
column4 int,
column5 int,
column6 int,
column7 int,
column8 int,
column9 int,
datevalue datetime)

insert removeDups
select 1,1,1,1,1,1,1,1,1,getdate()
waitfor delay '00:00:01'
insert removeDups
select 1,1,1,1,1,1,1,1,1,getdate()
waitfor delay '00:00:01'
insert removeDups
select 1,1,1,1,1,1,1,1,1,getdate()
waitfor delay '00:00:01'

insert removeDups
select 2,2,2,2,2,2,2,2,2,getdate()
waitfor delay '00:00:01'
insert removeDups
select 2,2,2,2,2,2,2,2,2,getdate()
waitfor delay '00:00:01'
insert removeDups
select 3,3,3,3,3,3,3,3,3,getdate()
waitfor delay '00:00:01'


delete from removeDups
where not exists
(select *
from ( select min(dateValue) as dateValue,column1,column2,column3,column4,column5,column6, column7, column8, column9
from removeDups
group by column1,column2,column3,column4,column5,column6, column7, column8, column9) as mins
where mins.dateValue = removeDups.dateValue
and mins.column1 = removeDups.column1
and mins.column2 = removeDups.column2
and mins.column3 = removeDups.column3
and mins.column4 = removeDups.column4
and mins.column5 = removeDups.column5
and mins.column6 = removeDups.column6
and mins.column7 = removeDups.column7
and mins.column8 = removeDups.column8
and mins.column9 = removeDups.column9)

select *
from removeDups

I figure once you finish this process you will probably want to hurt the person who gave you this design, even if it is yourself. Try to identify a key amongst the 68 columns and add a unique constraint so you can never get in this position again :)

Monday, March 12, 2012

How to duplicate table structure in Transactional replication?

Dear all:
After I create a new publication and its subscription by Transactional
replication in SQL server 2000,I want to add a column or alter a column
length in publication table and hope that the column can be duplicated to
subscription table.However,I find that Transactional replication in SQL
server 2000 can only replicate data and can not replicate table structure.
The replication type I use is "Transactional replication".
My problem is:
How to duplicate table structure in Transactional replication of SQL server
2000?
On 8 Jun, 04:45, gzwangyang <gzwangy...@.discussions.microsoft.com>
wrote:
> Dear all:
> After I create a new publication and its subscription by Transactional
> replication in SQL server 2000,I want to add a column or alter a column
> length in publication table and hope that the column can be duplicated to
> subscription table.However,I find that Transactional replication in SQL
> server 2000 can only replicate data and can not replicate table structure.
> The replication type I use is "Transactional replication".
> My problem is:
> How to duplicate table structure in Transactional replication of SQL server
> 2000?
What you're trying to do isn't strictly possible. You can't alter a
table that is published for replication.
You would need to drop replication, change the table and then re-
enable the replication with snapshot enabled. Provided your articles
snapshot attributes are correct it will create the table at the
subscriber.
Thanks
James
|||This is not correct. You can use sp_repladdcolumn and sp_repldropcolumn.
Altering a column is not straightforward but is achievable
(http://www.replicationanswers.com/AddColumn.asp).
Cheers,
Paul Ibison

how to duplicate my database?

Hello guys
I have to do a demo and I need to bring my database with me...how can I do
that. I have ms sql server 2000. Is there a way of executing a script or
something?
ThanksNaby wrote:

> I have to do a demo and I need to bring my database with me...how can
> I do that. I have ms sql server 2000. Is there a way of executing a
> script or something?
What I do is take a backup and restore that on the demo machine.
Fastest way to do it I think.
HTH,
Stijn Verrept.|||Dettach database copy/paste mdf and ldf file and attach.
This requires downtime.
But its better if you want to do it fast then to backup database and
restore it at other location
Regards|||Hi Naby,
you can Generate SQL scripts with help of enterprise manager. It will
create a scripts which will contain all tables, view, procedure, defaults an
d
rules of your database, you also customize the option in generating this
script .
with help of the script, you create same tables, procedures and all
present in the scripts. but data will not be there. To have data, Use DTS
transfer the data to text file and after running the scritps transfer the
data in text to database.
regards
Vinod.
"Naby" wrote:

> Hello guys
> I have to do a demo and I need to bring my database with me...how can I do
> that. I have ms sql server 2000. Is there a way of executing a script or
> something?
> Thanks|||You have to take care of order of tabled due to constraints while using
dts , its more tedious work
or you have to disable constraints that is not good idea for live
database.
Regards
Amish|||The following article describes various methods for implementing a standby
server, but some of them would also apply in your case:
http://vyaskn.tripod.com/maintainin..._sql_server.htm
One option the above article doesn't mention is detach / copy / reattaching
the database files:
http://support.microsoft.com/kb/224071
You didn't say if SQL Server is installed on your laptop, but the MSDE
version is one option:
http://www.microsoft.com/sql/editio...ss/default.mspx
"Naby" <Naby@.discussions.microsoft.com> wrote in message
news:459F468F-F207-4FBF-A289-13091612A185@.microsoft.com...
> Hello guys
> I have to do a demo and I need to bring my database with me...how can I do
> that. I have ms sql server 2000. Is there a way of executing a script or
> something?
> Thanks

How to duplicate Identity Column in SQL Server 2000?

Dear All:
The table for subscription is the same as the table for publication.
create table zt_company(company_id int identity(1,1) not for replication
primary key,companyname varchar(200),create_date datetime,modify_date
datetime)
If I use "not for replication" option when creating table,publication and
subscription is successful.However,company_id column of subscription table
loses identity attribute and primary key.
If I do not use "not for replication" option when creating table,publication
and subscription fails.
I want to keep "identity attribute and primary key" of identity column in
subscription table ,what should I do?
I use Enterprise manager for publication and subscription.
thanks.
You didn't mention what type of replication you're using, but presumably
you're talking about transactional? If so, you can set up queued updating
subscribers to maintain the identity attribute and have it set up ready for
failover - is that what you want?
Cheers,
Paul Ibison
|||Dear Paul Ibison:
Thanks for your help."queued updating subscribers" sucessfully solved my
problem and is what I want.
wangyang.
"Paul Ibison" wrote:

> You didn't mention what type of replication you're using, but presumably
> you're talking about transactional? If so, you can set up queued updating
> subscribers to maintain the identity attribute and have it set up ready for
> failover - is that what you want?
> Cheers,
> Paul Ibison
>

how to duplicate an entire database

I have built a template database which I'm finally pleased with, however I want to periodically duplicate the design - not data into a new database. How can I duplicate a database? I was hoping to right mouse, copy, then right mouse, paste, and then be prompted for the new name but no such luck.You cannot copy paste a database. You can write a script which copys and pastes the database by accecpting the database name as a parameter|||You cannot copy paste a database.
yep, I'm very clear on this.

You can write a script which copys and pastes the database by accecpting the database name as a parameter
Sorry for being so rash, but had I known how to write the script I wouldn't have posted my message. You might as well have answered with the single word "yes" as this would have been just as helpful.

example........please.|||Check this out, it may lead you in the right direction:

"C:\Program Files\Microsoft SQL Server\MSSQL\Upgrade\scptxfr.exe" /s <SERVER_NAME> /I /d pubs /r /f C:\test.sql|||in enterprise manager click tools (i think, I am at home no enterprise manager here) then Generate SQL Script it will pop a wizard that will walk you through generating a complete script of the database.

hope that helps

Tal McMahon|||And how is this better than using a native tool that would do everything for you?|||The scripts that EM generate are junk if you have any kind of relational integrity, unless of course you want to spend hours looking through it and straightening it out so it will actually work. I would look at the tool that rdjabarov pointed out.

If you have Visio, you could also easily reverse engineer the database, then create a new one from the data model. You would then have a nifty little Visio diagram for your database.

How to Duplicate a SQL Instance

Is there a way to duplicate a SQL instance existing on one machine to
another machine ? This is for debugging purpose of some applications
running against the databases on such instance without disturbing the
production machine. I need the exact SQL instance environment with all the
databases contained. If there is no such tool available, then what do you
recommend to accomplish this ?
TIA
MacThis article will give you some pointers:
http://vyaskn.tripod.com/moving_sql_server.htm
--
HTH,
Vyas, MVP (SQL Server)
http://vyaskn.tripod.com/
What hardware is your SQL Server running on?
http://vyaskn.tripod.com/poll.htm
"Mac Vazehgoo" <mahmood.vazehgoo@.unisys.com> wrote in message
news:bql5al$kqa$1@.si05.rsvl.unisys.com...
Is there a way to duplicate a SQL instance existing on one machine to
another machine ? This is for debugging purpose of some applications
running against the databases on such instance without disturbing the
production machine. I need the exact SQL instance environment with all the
databases contained. If there is no such tool available, then what do you
recommend to accomplish this ?
TIA
Mac|||Thanks Vyas. Lots of good info in there..
// Mac
"Narayana Vyas Kondreddi" <answer_me@.hotmail.com> wrote in message
news:ehEWyDcuDHA.536@.tk2msftngp13.phx.gbl...
> This article will give you some pointers:
> http://vyaskn.tripod.com/moving_sql_server.htm
> --
> HTH,
> Vyas, MVP (SQL Server)
> http://vyaskn.tripod.com/
> What hardware is your SQL Server running on?
> http://vyaskn.tripod.com/poll.htm
>
> "Mac Vazehgoo" <mahmood.vazehgoo@.unisys.com> wrote in message
> news:bql5al$kqa$1@.si05.rsvl.unisys.com...
> Is there a way to duplicate a SQL instance existing on one machine to
> another machine ? This is for debugging purpose of some applications
> running against the databases on such instance without disturbing the
> production machine. I need the exact SQL instance environment with all
the
> databases contained. If there is no such tool available, then what do
you
> recommend to accomplish this ?
> TIA
> Mac
>
>

How to duplicate a row in SQL Table using a single query

Hi,
I have a table with about 50 fields including intCompanyId as the
table identity.
I want to be able to use one query (or a few in a stored proc to
duplicate a specific row in this table.
I do not want to list the fields in case some are added later.
Ex : insert into T_Company select * from T_Company where intCompanyId
= 17
This query gives me an error :
An explicit value for the identity column in table 'T_Company' can
only be specified when a column list is used and IDENTITY_INSERT is
ON.
Even if I turn it OFF it will not let it insert it since it will have
the same intCompanyId as the other one.
Can some one help me?
Thanks
Alain
> I want to be able to use one query (or a few in a stored proc to
> duplicate a specific row in this table.
Why on earth would you want to _duplicate_ a row in a table? It makes no
sense to do that.

> I do not want to list the fields in case some are added later.
Good practice is to *always* list the columns, never use SELECT * in
production code. This should actually make the code easier to maintain and
debug if the DDL changes because your code will fail safe. INSERT ... SELECT
* is dangerous because it assumes a fixed sequential order to the columns
and that is something you shouldn't need to worry about and isn't always
easy to have complete control over.
David Portas
SQL Server MVP
|||I am really curious to know why you want to do something like that. It's like
asking us how to mess up your table...
"Alain Filiatrault" wrote:

> Hi,
> I have a table with about 50 fields including intCompanyId as the
> table identity.
> I want to be able to use one query (or a few in a stored proc to
> duplicate a specific row in this table.
> I do not want to list the fields in case some are added later.
> Ex : insert into T_Company select * from T_Company where intCompanyId
> = 17
> This query gives me an error :
> An explicit value for the identity column in table 'T_Company' can
> only be specified when a column list is used and IDENTITY_INSERT is
> ON.
> Even if I turn it OFF it will not let it insert it since it will have
> the same intCompanyId as the other one.
> Can some one help me?
> Thanks
> Alain
>
|||Alain,
Like Susan, I wonder why you want to do this. But there are two
things going on here. First, because there is an identity column, you
can't insert values into every column of the table, as insert into
T_Company select * ... would do. Because you say that you can't insert
even with IDENTITY_INSERT set to ON (I assume you meant "Even if I turn
it ON", not OFF), there must be a unique or primary key constraint on
the identity column. I would hope that constraint is in place for a
reason, and that constraint is preventing you from entering duplicate
information (in the identity column, at least) into the table. Your
desire to add a duplicate row to the table is at odds with the desire of
the database designer, who designed the database to prevent anyone from
doing what you want to do.
Steve Kass
Drew University
Alain Filiatrault wrote:

>Hi,
>I have a table with about 50 fields including intCompanyId as the
>table identity.
>I want to be able to use one query (or a few in a stored proc to
>duplicate a specific row in this table.
>I do not want to list the fields in case some are added later.
>Ex : insert into T_Company select * from T_Company where intCompanyId
>= 17
>This query gives me an error :
>An explicit value for the identity column in table 'T_Company' can
>only be specified when a column list is used and IDENTITY_INSERT is
>ON.
>Even if I turn it OFF it will not let it insert it since it will have
>the same intCompanyId as the other one.
>Can some one help me?
>Thanks
>Alain
>
|||Guys, Guys, Guys,
It never ceases to amaze me how so many questions get answered with another question... instead of an answer.
I have the same question, but I think what the original author is trying to do here is duplicate the row while allowing the identity/key to grow on its own... without having to list every other column in the table. (Hence the * he is wanting to use). Furthermore, the author would like to accomplish this in a single SQL statement.
Afterall, a primary key is a primary key, and a unique identifier is a unique identifer.
It seems to me this would be a capability often persued by any seasoned and active SQL programmer. And thus, I would expect such functionality from a seasoned language (aka, sql)
With the previous assumption having been made, does anyone have the answer? Is there a way to duplicate everything in a row except for any autonumbering identifiers while allowing those autonumbers to autonumber as they were designed to (without listing every single other column in the table)?|||Yes, this is a an activity that I end up doing regularly. Typically
when I am writing a stored procedure and I want to get a working set
of data into a temporary table and have the temporary table use its
own identity key.
There is no straightforward way that I know of doing this but I use a
technique that Dan Guzman MVP gave me some time back which I think is
very neat.
--create temp table with resultset columns
SELECT *
INTO #MetaData
FROM MyBigTable
...or...
select * (where all columns are listed and have column names)
into #MetaData
from [my_bespoke_query_joining_several_tables]
--list meta data
SELECT *
FROM tempdb.INFORMATION_SCHEMA.COLUMNS
WHERE
OBJECT_ID(
(
-- Use the collation of either your tempdb or your current db here...
-- Collations must match. Experiment.
+ QUOTENAME(TABLE_CATALOG) COLLATE
SQL_Latin1_General_CP1_CI_AS
+ N'.' COLLATE SQL_Latin1_General_CP1_CI_AS
+ QUOTENAME(TABLE_SCHEMA) COLLATE
SQL_Latin1_General_CP1_CI_AS
+ N'.' COLLATE SQL_Latin1_General_CP1_CI_AS
+ QUOTENAME(TABLE_NAME) COLLATE
SQL_Latin1_General_CP1_CI_AS)
) =
OBJECT_ID('tempdb.dbo.#MetaData' COLLATE
SQL_Latin1_General_CP1_CI_AS) ORDER BY ORDINAL_POSITION
-- More specific case where I want to prepare a create table statement
for a
-- temp table that will hold the result set from a stored procedure
because the
-- temporary table must exist before you can use a statement like...
-- INSERT #my_tbl
-- EXECmy_proc
SELECT case ordinal_position
when 1 then ' '
else ' ,'
end
+ quotename(column_name) collate SQL_Latin1_General_CP1_CI_AS +
' '
+ case data_type
when 'int' then data_type
when 'char' then data_type + '(' + convert(varchar(5),
character_maximum_length) + ')'
when 'varchar' then data_type + '(' + convert(varchar(5),
character_maximum_length) + ')'
when 'nvarchar' then 'varchar(' + convert(varchar(5),
character_maximum_length) + ')'
when 'decimal' then data_type + '(' + convert(varchar(5),
numeric_precision) + ',' + convert(varchar(5), numeric_scale) + ')'
when 'smallint' then 'int'
when 'datetime' then data_type
when 'smalldatetime' then 'datetime'
else 'datatype not handled'
end
FROM tempdb.INFORMATION_SCHEMA.COLUMNS
WHERE
OBJECT_ID(
(
-- Use the collation of either the tempdb or the current db here...
-- Collations must match. Experiment.
+ QUOTENAME(TABLE_CATALOG) COLLATE
SQL_Latin1_General_CP1_CI_AS
+ N'.' COLLATE SQL_Latin1_General_CP1_CI_AS
+ QUOTENAME(TABLE_SCHEMA) COLLATE
SQL_Latin1_General_CP1_CI_AS
+ N'.' COLLATE SQL_Latin1_General_CP1_CI_AS
+ QUOTENAME(TABLE_NAME) COLLATE
SQL_Latin1_General_CP1_CI_AS)
) =
OBJECT_ID('tempdb.dbo.#MetaData' COLLATE
SQL_Latin1_General_CP1_CI_AS) ORDER BY ORDINAL_POSITION
In fact, if I am writing a stored proc that will later almost
certainly become an input to a further aggregated proc, I always put
in an additional parameter that allows the meta-data of the proc to be
returned as an additional result set.
I think this should give you a nice workaround that at worst means a
bit of cutting and pasting.
Regards
Liam
TheSqlGuy <TheSqlGuy.1dqkdq@.mail.mcse.ms> wrote in message news:<TheSqlGuy.1dqkdq@.mail.mcse.ms>...
> Guys, Guys, Guys,
> It never ceases to amaze me how so many questions get answered with
> another question... instead of an answer.
> I have the same question, but I think what the original author is
> trying to do here is duplicate the row while allowing the identity/key
> to grow on its own... without having to list every other column in the
> table. (Hence the * he is wanting to use). Furthermore, the author
> would like to accomplish this in a single SQL statement.
> Afterall, a primary key is a primary key, and a unique identifier is a
> unique identifer.
> It seems to me this would be a capability often persued by any seasoned
> and active SQL programmer. And thus, I would expect such functionality
> from a seasoned language (aka, sql)
> With the previous assumption having been made, does anyone have the
> answer? Is there a way to duplicate everything in a row except for any
> autonumbering identifiers while allowing those autonumbers to
> autonumber as they were designed to (without listing every single other
> column in the table)?
|||> It never ceases to amaze me how so many questions get answered with
> another question... instead of an answer.
Answering a question directly isn't always the appropriate and professional
way to help someone who needs it. If someone asked you "Where can I get a
gun so I can shoot myself?" would you just answer the direct question or
would you offer some more constructive suggestions? The OP was asking how to
do something that will destroy the integrity of his data (if indeed it had
any to start with). My response was to ask for more information about his
actual business requirement so I could advise him better. Unfortunately
there are a lot of people posting to this group who are desperately trying
to shoot themselves in the foot. Not all of us want to help them to do it!

> It seems to me this would be a capability often persued by any seasoned
> and active SQL programmer.
Not by a *good* SQL programmer!
David Portas
SQL Server MVP
|||"David Portas" <REMOVE_BEFORE_REPLYING_dportas@.acm.org> wrote in message
news:hO-dnef2Xcp_B_jcRVn-iw@.giganews.com...

> If someone asked you "Where can I get a
> gun so I can shoot myself?" would you just answer the direct question or
> would you offer some more constructive suggestions?
Depends on the person.
|||Also depends on what the original poster meant by "duplicate". I have
often had users ask me for funtionality to "duplicate" an order and
what they really mean is that they want the original order used as a
template to produce a new order - which is fair enough. Of course, it
would suggest that such functionality was an afterthought (which it
often is) and should have been factored into the original design.
An ability to appreciate the difference between the syntactic
precision of gun mechanics(parsed code) and the semantic ambiguity of
target shooting(meeting users requirements and expectations) is a very
useful skill.
Regards
Liam
"Mark Wilden" <mark@.mwilden.com> wrote in message news:<TfSdnZTmCJhXW_jcRVn-rw@.sti.net>...
> "David Portas" <REMOVE_BEFORE_REPLYING_dportas@.acm.org> wrote in message
> news:hO-dnef2Xcp_B_jcRVn-iw@.giganews.com...
>
> Depends on the person.

How to duplicate a row in SQL Table using a single query

Hi,
I have a table with about 50 fields including intCompanyId as the
table identity.
I want to be able to use one query (or a few in a stored proc to
duplicate a specific row in this table.
I do not want to list the fields in case some are added later.
Ex : insert into T_Company select * from T_Company where intCompanyId
= 17
This query gives me an error :
An explicit value for the identity column in table 'T_Company' can
only be specified when a column list is used and IDENTITY_INSERT is
ON.
Even if I turn it OFF it will not let it insert it since it will have
the same intCompanyId as the other one.
Can some one help me?
Thanks
Alain> I want to be able to use one query (or a few in a stored proc to
> duplicate a specific row in this table.
Why on earth would you want to _duplicate_ a row in a table? It makes no
sense to do that.
> I do not want to list the fields in case some are added later.
Good practice is to *always* list the columns, never use SELECT * in
production code. This should actually make the code easier to maintain and
debug if the DDL changes because your code will fail safe. INSERT ... SELECT
* is dangerous because it assumes a fixed sequential order to the columns
and that is something you shouldn't need to worry about and isn't always
easy to have complete control over.
--
David Portas
SQL Server MVP
--|||I am really curious to know why you want to do something like that. It's like
asking us how to mess up your table...
"Alain Filiatrault" wrote:
> Hi,
> I have a table with about 50 fields including intCompanyId as the
> table identity.
> I want to be able to use one query (or a few in a stored proc to
> duplicate a specific row in this table.
> I do not want to list the fields in case some are added later.
> Ex : insert into T_Company select * from T_Company where intCompanyId
> = 17
> This query gives me an error :
> An explicit value for the identity column in table 'T_Company' can
> only be specified when a column list is used and IDENTITY_INSERT is
> ON.
> Even if I turn it OFF it will not let it insert it since it will have
> the same intCompanyId as the other one.
> Can some one help me?
> Thanks
> Alain
>|||Alain,
Like Susan, I wonder why you want to do this. But there are two
things going on here. First, because there is an identity column, you
can't insert values into every column of the table, as insert into
T_Company select * ... would do. Because you say that you can't insert
even with IDENTITY_INSERT set to ON (I assume you meant "Even if I turn
it ON", not OFF), there must be a unique or primary key constraint on
the identity column. I would hope that constraint is in place for a
reason, and that constraint is preventing you from entering duplicate
information (in the identity column, at least) into the table. Your
desire to add a duplicate row to the table is at odds with the desire of
the database designer, who designed the database to prevent anyone from
doing what you want to do.
Steve Kass
Drew University
Alain Filiatrault wrote:
>Hi,
>I have a table with about 50 fields including intCompanyId as the
>table identity.
>I want to be able to use one query (or a few in a stored proc to
>duplicate a specific row in this table.
>I do not want to list the fields in case some are added later.
>Ex : insert into T_Company select * from T_Company where intCompanyId
>= 17
>This query gives me an error :
>An explicit value for the identity column in table 'T_Company' can
>only be specified when a column list is used and IDENTITY_INSERT is
>ON.
>Even if I turn it OFF it will not let it insert it since it will have
>the same intCompanyId as the other one.
>Can some one help me?
>Thanks
>Alain
>|||Guys, Guys, Guys,
It never ceases to amaze me how so many questions get answered wit
another question... instead of an answer.
I have the same question, but I think what the original author i
trying to do here is duplicate the row while allowing the identity/ke
to grow on its own... without having to list every other column in th
table. (Hence the * he is wanting to use). Furthermore, the autho
would like to accomplish this in a single SQL statement.
Afterall, a primary key is a primary key, and a unique identifier is
unique identifer.
It seems to me this would be a capability often persued by any seasone
and active SQL programmer. And thus, I would expect such functionalit
from a seasoned language (aka, sql)
With the previous assumption having been made, does anyone have th
answer? Is there a way to duplicate everything in a row except for an
autonumbering identifiers while allowing those autonumbers t
autonumber as they were designed to (without listing every single othe
column in the table)
-
TheSqlGu
----
Posted via http://www.mcse.m
----
View this thread: http://www.mcse.ms/message1119654.htm|||Yes, this is a an activity that I end up doing regularly. Typically
when I am writing a stored procedure and I want to get a working set
of data into a temporary table and have the temporary table use its
own identity key.
There is no straightforward way that I know of doing this but I use a
technique that Dan Guzman MVP gave me some time back which I think is
very neat.
--create temp table with resultset columns
SELECT *
INTO #MetaData
FROM MyBigTable
...or...
select * (where all columns are listed and have column names)
into #MetaData
from [my_bespoke_query_joining_several_tables]
--list meta data
SELECT *
FROM tempdb.INFORMATION_SCHEMA.COLUMNS
WHERE
OBJECT_ID(
(
-- Use the collation of either your tempdb or your current db here...
-- Collations must match. Experiment.
+ QUOTENAME(TABLE_CATALOG) COLLATE
SQL_Latin1_General_CP1_CI_AS
+ N'.' COLLATE SQL_Latin1_General_CP1_CI_AS
+ QUOTENAME(TABLE_SCHEMA) COLLATE
SQL_Latin1_General_CP1_CI_AS
+ N'.' COLLATE SQL_Latin1_General_CP1_CI_AS
+ QUOTENAME(TABLE_NAME) COLLATE
SQL_Latin1_General_CP1_CI_AS)
) = OBJECT_ID('tempdb.dbo.#MetaData' COLLATE
SQL_Latin1_General_CP1_CI_AS) ORDER BY ORDINAL_POSITION
-- More specific case where I want to prepare a create table statement
for a
-- temp table that will hold the result set from a stored procedure
because the
-- temporary table must exist before you can use a statement like...
-- INSERT #my_tbl
-- EXECmy_proc
SELECT case ordinal_position
when 1 then ' '
else ' ,'
end
+ quotename(column_name) collate SQL_Latin1_General_CP1_CI_AS +
' '
+ case data_type
when 'int' then data_type
when 'char' then data_type + '(' + convert(varchar(5),
character_maximum_length) + ')'
when 'varchar' then data_type + '(' + convert(varchar(5),
character_maximum_length) + ')'
when 'nvarchar' then 'varchar(' + convert(varchar(5),
character_maximum_length) + ')'
when 'decimal' then data_type + '(' + convert(varchar(5),
numeric_precision) + ',' + convert(varchar(5), numeric_scale) + ')'
when 'smallint' then 'int'
when 'datetime' then data_type
when 'smalldatetime' then 'datetime'
else 'datatype not handled'
end
FROM tempdb.INFORMATION_SCHEMA.COLUMNS
WHERE
OBJECT_ID(
(
-- Use the collation of either the tempdb or the current db here...
-- Collations must match. Experiment.
+ QUOTENAME(TABLE_CATALOG) COLLATE
SQL_Latin1_General_CP1_CI_AS
+ N'.' COLLATE SQL_Latin1_General_CP1_CI_AS
+ QUOTENAME(TABLE_SCHEMA) COLLATE
SQL_Latin1_General_CP1_CI_AS
+ N'.' COLLATE SQL_Latin1_General_CP1_CI_AS
+ QUOTENAME(TABLE_NAME) COLLATE
SQL_Latin1_General_CP1_CI_AS)
) = OBJECT_ID('tempdb.dbo.#MetaData' COLLATE
SQL_Latin1_General_CP1_CI_AS) ORDER BY ORDINAL_POSITION
In fact, if I am writing a stored proc that will later almost
certainly become an input to a further aggregated proc, I always put
in an additional parameter that allows the meta-data of the proc to be
returned as an additional result set.
I think this should give you a nice workaround that at worst means a
bit of cutting and pasting.
Regards
Liam
TheSqlGuy <TheSqlGuy.1dqkdq@.mail.mcse.ms> wrote in message news:<TheSqlGuy.1dqkdq@.mail.mcse.ms>...
> Guys, Guys, Guys,
> It never ceases to amaze me how so many questions get answered with
> another question... instead of an answer.
> I have the same question, but I think what the original author is
> trying to do here is duplicate the row while allowing the identity/key
> to grow on its own... without having to list every other column in the
> table. (Hence the * he is wanting to use). Furthermore, the author
> would like to accomplish this in a single SQL statement.
> Afterall, a primary key is a primary key, and a unique identifier is a
> unique identifer.
> It seems to me this would be a capability often persued by any seasoned
> and active SQL programmer. And thus, I would expect such functionality
> from a seasoned language (aka, sql)
> With the previous assumption having been made, does anyone have the
> answer? Is there a way to duplicate everything in a row except for any
> autonumbering identifiers while allowing those autonumbers to
> autonumber as they were designed to (without listing every single other
> column in the table)?|||> It never ceases to amaze me how so many questions get answered with
> another question... instead of an answer.
Answering a question directly isn't always the appropriate and professional
way to help someone who needs it. If someone asked you "Where can I get a
gun so I can shoot myself?" would you just answer the direct question or
would you offer some more constructive suggestions? The OP was asking how to
do something that will destroy the integrity of his data (if indeed it had
any to start with). My response was to ask for more information about his
actual business requirement so I could advise him better. Unfortunately
there are a lot of people posting to this group who are desperately trying
to shoot themselves in the foot. Not all of us want to help them to do it!
> It seems to me this would be a capability often persued by any seasoned
> and active SQL programmer.
Not by a *good* SQL programmer!
--
David Portas
SQL Server MVP
--|||"David Portas" <REMOVE_BEFORE_REPLYING_dportas@.acm.org> wrote in message
news:hO-dnef2Xcp_B_jcRVn-iw@.giganews.com...
> If someone asked you "Where can I get a
> gun so I can shoot myself?" would you just answer the direct question or
> would you offer some more constructive suggestions?
Depends on the person.|||Also depends on what the original poster meant by "duplicate". I have
often had users ask me for funtionality to "duplicate" an order and
what they really mean is that they want the original order used as a
template to produce a new order - which is fair enough. Of course, it
would suggest that such functionality was an afterthought (which it
often is) and should have been factored into the original design.
An ability to appreciate the difference between the syntactic
precision of gun mechanics(parsed code) and the semantic ambiguity of
target shooting(meeting users requirements and expectations) is a very
useful skill.
Regards
Liam
"Mark Wilden" <mark@.mwilden.com> wrote in message news:<TfSdnZTmCJhXW_jcRVn-rw@.sti.net>...
> "David Portas" <REMOVE_BEFORE_REPLYING_dportas@.acm.org> wrote in message
> news:hO-dnef2Xcp_B_jcRVn-iw@.giganews.com...
> > If someone asked you "Where can I get a
> > gun so I can shoot myself?" would you just answer the direct question or
> > would you offer some more constructive suggestions?
> Depends on the person.

How to duplicate a DataBase?

Hi,
In my Win2003 Server, SQL Server 2000, we have a DataBase MyDataBase for
testing.
Now I want to make a NewDataBase which is based on MyDataBase.
Is it possilbe to duplicate a new database from MyDataBase?
Thanks for help.
JasonJason
Sure, BACKUP DATABASE MyDataBase and RESTORE MyDataBase_New WITH MOVE
option (See details in the BOL)
"Jason Huang" <JasonHuang8888@.hotmail.com> wrote in message
news:%23X%23ZsIoRGHA.2156@.tk2msftngp13.phx.gbl...
> Hi,
> In my Win2003 Server, SQL Server 2000, we have a DataBase MyDataBase for
> testing.
> Now I want to make a NewDataBase which is based on MyDataBase.
> Is it possilbe to duplicate a new database from MyDataBase?
> Thanks for help.
>
> Jason
>

How to duplicate a DataBase?

Hi,
In my Win2003 Server, SQL Server 2000, we have a DataBase MyDataBase for
testing.
Now I want to make a NewDataBase which is based on MyDataBase.
Is it possilbe to duplicate a new database from MyDataBase?
Thanks for help.
Jason
Jason
Sure, BACKUP DATABASE MyDataBase and RESTORE MyDataBase_New WITH MOVE
option (See details in the BOL)
"Jason Huang" <JasonHuang8888@.hotmail.com> wrote in message
news:%23X%23ZsIoRGHA.2156@.tk2msftngp13.phx.gbl...
> Hi,
> In my Win2003 Server, SQL Server 2000, we have a DataBase MyDataBase for
> testing.
> Now I want to make a NewDataBase which is based on MyDataBase.
> Is it possilbe to duplicate a new database from MyDataBase?
> Thanks for help.
>
> Jason
>

How to duplicate a database?

Hi:
I have a database for my asp.net application and now I want to create
another database with everything same, including the data, except the
database name(this database will be used by another asp.net application).
How can I do this programmingly? I am using C#.
Thank you very much!
Quentin H.You can restore from a recent backup and specify the new name. Also, you can
detach, copy the mdf file, and then re-attach under a new name. How to do
this from C# ? You could use data management objects (DMO), but I would
suggest just writing a stored procedure and calling the SP from C#.
"Quentin Huo" <q.huo@.manyworlds.com> wrote in message
news:%23QcXgaiZFHA.3328@.TK2MSFTNGP09.phx.gbl...
> Hi:
> I have a database for my asp.net application and now I want to create
> another database with everything same, including the data, except the
> database name(this database will be used by another asp.net application).
> How can I do this programmingly? I am using C#.
> Thank you very much!
> Quentin H.
>|||Thank you for your quick reply!
Also, I want to ask how I can do if I want to duplicate the database, but
without any data. Do I need to use DMO? I tried this before by DTS, but
everytime when I did this, some definition of fields were changed. For
example, there is a field named "adddate" which default value is
"getDate()". However, after I duplicate it from DTS, the default value
(getdate()) was lost. Maybe I lost something when I did DTS?
Thanks
Q.
"JT" <someone@.microsoft.com> wrote in message
news:%23fQMymiZFHA.3364@.TK2MSFTNGP12.phx.gbl...
> You can restore from a recent backup and specify the new name. Also, you
> can
> detach, copy the mdf file, and then re-attach under a new name. How to do
> this from C# ? You could use data management objects (DMO), but I would
> suggest just writing a stored procedure and calling the SP from C#.
> "Quentin Huo" <q.huo@.manyworlds.com> wrote in message
> news:%23QcXgaiZFHA.3328@.TK2MSFTNGP09.phx.gbl...
>|||The easiest way of doing this is the restore from a recent backup ! You
can restore from a backup using DMO or TSQL with ADO !
Your best bet if you use DTS is to use copy object job. Try the copy
database wizard and reuse the DTS made by it !
Pollus Brodeur

How to duplicate a DataBase?

Hi,
In my Win2003 Server, SQL Server 2000, we have a DataBase MyDataBase for
testing.
Now I want to make a NewDataBase which is based on MyDataBase.
Is it possilbe to duplicate a new database from MyDataBase?
Thanks for help.
JasonJason
Sure, BACKUP DATABASE MyDataBase and RESTORE MyDataBase_New WITH MOVE
option (See details in the BOL)
"Jason Huang" <JasonHuang8888@.hotmail.com> wrote in message
news:%23X%23ZsIoRGHA.2156@.tk2msftngp13.phx.gbl...
> Hi,
> In my Win2003 Server, SQL Server 2000, we have a DataBase MyDataBase for
> testing.
> Now I want to make a NewDataBase which is based on MyDataBase.
> Is it possilbe to duplicate a new database from MyDataBase?
> Thanks for help.
>
> Jason
>

how to drop index?

I want to check for duplicate data, if got duplicate data, then the
table will be dropped, if dun have, the table will not be dropped, but
the constraint and the indexed within the table will be dropped, and
an mail notification will be send. Below is my syntax uwing stored
procedure,when i run this stored procedure, it get an error message
:"Incorrect syntax near 'PK_Rewards_CatalogProducts_CS' ", Can anyone
tell me the correct way to drop the constraint and index?
if not exists(select catalog_code, count(catalog_code) from rd_awards
group
by catalog_code having
count(catalog_code) > 1)
begin
drop table [dbo].[Rewards_CatalogProducts_CS]
end
else
if exists(select catalog_code, count(catalog_code) from rd_awards
group
by catalog_code having
count(catalog_code) > 1)
begin
ALTER TABLE [dbo].[Rewards_CatalogProducts_CS]
DROP CONSTRAINT [PK_Rewards_CatalogProducts_CS]
GO
ALTER TABLE [dbo].[Rewards_CatalogProducts_CS]
DROP INDEX [idx_categorycode]
GO
ALTER TABLE [dbo].[Rewards_CatalogProducts_CS]
DROP INDEX [idx_g_org_SupplierId]
use master
declare @.FROM NVARCHAR(4000),
@.FROM_NAME NVARCHAR(4000),
@.TO NVARCHAR(4000),
@.CC NVARCHAR(4000),
@.BCC NVARCHAR(4000),
@.priority NVARCHAR(10),
@.subject NVARCHAR(4000),
@.message NVARCHAR(4000),
@.type NVARCHAR(100),
@.attachments NVARCHAR(4000),
@.codepage INT,
@.rc INT
select @.FROM = N'sqlmail@.cyber-village.net',
@.FROM_NAME = N'ChangMian',
@.TO = N'tchangmian@.yahoo.com.sg',
@.CC = N'changmian@.cyber-village.net',
@.priority = N'High',
@.subject = N'headache',
@.message = N'&
Hello SQL Server SMTP SQL
Mail
',
@.type = N'text/html',
@.attachments = N'',
@.codepage = 0
exec @.rc = master.dbo.xp_smtp_sendmail
@.FROM = @.FROM,
@.TO = @.TO,
@.CC = @.CC,
@.priority = @.priority,
@.subject = @.subject,
@.message = @.message,
@.type = @.type,
@.attachments = @.attachments,
@.codepage = @.codepage,
@.server = N'mail.cyber-village.net'
select RC = @.rc
endHI!
Not the Alter Statement is the problem. The problem is that The BEGIN has no
coresponding END in your IF Statement, because you are dividing your script
in several batches with the GO Statement.
IF BEGIN END has to be in one Batch and with GO you are starting a new Batch
which is the unit of Parsing and Compilation for SQL Server. GO is not a
SQL Statement. It tells Query Analyzer, osql,... to seperate the script into
units which are send separately to SQL Server (see BOL)
Just remove the GO statements, then it should work. They are not necessary
in your case.
Cheers,
Herbert
"tchangmian" <tchangmian@.yahoo.com.sg> schrieb im Newsbeitrag
news:6447ee25.0410070127.2321f3f1@.posting.google.com...
> I want to check for duplicate data, if got duplicate data, then the
> table will be dropped, if dun have, the table will not be dropped, but
> the constraint and the indexed within the table will be dropped, and
> an mail notification will be send. Below is my syntax uwing stored
> procedure,when i run this stored procedure, it get an error message
> :"Incorrect syntax near 'PK_Rewards_CatalogProducts_CS' ", Can anyone
> tell me the correct way to drop the constraint and index?
> if not exists(select catalog_code, count(catalog_code) from rd_awards
> group
> by catalog_code having
> count(catalog_code) > 1)
> begin
> drop table [dbo].[Rewards_CatalogProducts_CS]
> end
>
> else
> if exists(select catalog_code, count(catalog_code) from rd_awards
> group
> by catalog_code having
> count(catalog_code) > 1)
> begin
> ALTER TABLE [dbo].[Rewards_CatalogProducts_CS]
> DROP CONSTRAINT [PK_Rewards_CatalogProducts_CS]
> GO
> ALTER TABLE [dbo].[Rewards_CatalogProducts_CS]
> DROP INDEX [idx_categorycode]
> GO
> ALTER TABLE [dbo].[Rewards_CatalogProducts_CS]
> DROP INDEX [idx_g_org_SupplierId]
>
> use master
> declare @.FROM NVARCHAR(4000),
> @.FROM_NAME NVARCHAR(4000),
> @.TO NVARCHAR(4000),
> @.CC NVARCHAR(4000),
> @.BCC NVARCHAR(4000),
> @.priority NVARCHAR(10),
> @.subject NVARCHAR(4000),
> @.message NVARCHAR(4000),
> @.type NVARCHAR(100),
> @.attachments NVARCHAR(4000),
> @.codepage INT,
> @.rc INT
> select @.FROM = N'sqlmail@.cyber-village.net',
> @.FROM_NAME = N'ChangMian',
> @.TO = N'tchangmian@.yahoo.com.sg',
> @.CC = N'changmian@.cyber-village.net',
> @.priority = N'High',
> @.subject = N'headache',
> @.message = N'&
Hello SQL Server SMTP SQL
> Mail
',
> @.type = N'text/html',
> @.attachments = N'',
> @.codepage = 0
> exec @.rc = master.dbo.xp_smtp_sendmail
> @.FROM = @.FROM,
> @.TO = @.TO,
> @.CC = @.CC,
> @.priority = @.priority,
> @.subject = @.subject,
> @.message = @.message,
> @.type = @.type,
> @.attachments = @.attachments,
> @.codepage = @.codepage,
> @.server = N'mail.cyber-village.net'
> select RC = @.rc
> end

how to drop index?

I want to check for duplicate data, if got duplicate data, then the
table will be dropped, if dun have, the table will not be dropped, but
the constraint and the indexed within the table will be dropped, and
an mail notification will be send. Below is my syntax uwing stored
procedure,when i run this stored procedure, it get an error message
:"Incorrect syntax near 'PK_Rewards_CatalogProducts_CS' ", Can anyone
tell me the correct way to drop the constraint and index?
if not exists(select catalog_code, count(catalog_code) from rd_awards
group
by catalog_code having
count(catalog_code) > 1)
begin
drop table [dbo].[Rewards_CatalogProducts_CS]
end
else
if exists(select catalog_code, count(catalog_code) from rd_awards
group
by catalog_code having
count(catalog_code) > 1)
begin
ALTER TABLE [dbo].[Rewards_CatalogProducts_CS]
DROP CONSTRAINT [PK_Rewards_CatalogProducts_CS]
GO
ALTER TABLE [dbo].[Rewards_CatalogProducts_CS]
DROP INDEX [idx_categorycode]
GO
ALTER TABLE [dbo].[Rewards_CatalogProducts_CS]
DROP INDEX [idx_g_org_SupplierId]
use master
declare @.FROM NVARCHAR(4000),
@.FROM_NAME NVARCHAR(4000),
@.TO NVARCHAR(4000),
@.CC NVARCHAR(4000),
@.BCC NVARCHAR(4000),
@.priority NVARCHAR(10),
@.subject NVARCHAR(4000),
@.message NVARCHAR(4000),
@.type NVARCHAR(100),
@.attachments NVARCHAR(4000),
@.codepage INT,
@.rc INT
select @.FROM = N'sqlmail@.cyber-village.net',
@.FROM_NAME = N'ChangMian',
@.TO = N'tchangmian@.yahoo.com.sg',
@.CC = N'changmian@.cyber-village.net',
@.priority = N'High',
@.subject = N'headache',
@.message = N'<HTML><H1>Hello SQL Server SMTP SQL
Mail</H1></HTML>',
@.type = N'text/html',
@.attachments = N'',
@.codepage = 0
exec @.rc = master.dbo.xp_smtp_sendmail
@.FROM = @.FROM,
@.TO = @.TO,
@.CC = @.CC,
@.priority = @.priority,
@.subject = @.subject,
@.message = @.message,
@.type = @.type,
@.attachments = @.attachments,
@.codepage = @.codepage,
@.server = N'mail.cyber-village.net'
select RC = @.rc
end
HI!
Not the Alter Statement is the problem. The problem is that The BEGIN has no
coresponding END in your IF Statement, because you are dividing your script
in several batches with the GO Statement.
IF BEGIN END has to be in one Batch and with GO you are starting a new Batch
which is the unit of Parsing and Compilation for SQL Server. GO is not a
SQL Statement. It tells Query Analyzer, osql,... to seperate the script into
units which are send separately to SQL Server (see BOL)
Just remove the GO statements, then it should work. They are not necessary
in your case.
Cheers,
Herbert
"tchangmian" <tchangmian@.yahoo.com.sg> schrieb im Newsbeitrag
news:6447ee25.0410070127.2321f3f1@.posting.google.c om...
> I want to check for duplicate data, if got duplicate data, then the
> table will be dropped, if dun have, the table will not be dropped, but
> the constraint and the indexed within the table will be dropped, and
> an mail notification will be send. Below is my syntax uwing stored
> procedure,when i run this stored procedure, it get an error message
> :"Incorrect syntax near 'PK_Rewards_CatalogProducts_CS' ", Can anyone
> tell me the correct way to drop the constraint and index?
> if not exists(select catalog_code, count(catalog_code) from rd_awards
> group
> by catalog_code having
> count(catalog_code) > 1)
> begin
> drop table [dbo].[Rewards_CatalogProducts_CS]
> end
>
> else
> if exists(select catalog_code, count(catalog_code) from rd_awards
> group
> by catalog_code having
> count(catalog_code) > 1)
> begin
> ALTER TABLE [dbo].[Rewards_CatalogProducts_CS]
> DROP CONSTRAINT [PK_Rewards_CatalogProducts_CS]
> GO
> ALTER TABLE [dbo].[Rewards_CatalogProducts_CS]
> DROP INDEX [idx_categorycode]
> GO
> ALTER TABLE [dbo].[Rewards_CatalogProducts_CS]
> DROP INDEX [idx_g_org_SupplierId]
>
> use master
> declare @.FROM NVARCHAR(4000),
> @.FROM_NAME NVARCHAR(4000),
> @.TO NVARCHAR(4000),
> @.CC NVARCHAR(4000),
> @.BCC NVARCHAR(4000),
> @.priority NVARCHAR(10),
> @.subject NVARCHAR(4000),
> @.message NVARCHAR(4000),
> @.type NVARCHAR(100),
> @.attachments NVARCHAR(4000),
> @.codepage INT,
> @.rc INT
> select @.FROM = N'sqlmail@.cyber-village.net',
> @.FROM_NAME = N'ChangMian',
> @.TO = N'tchangmian@.yahoo.com.sg',
> @.CC = N'changmian@.cyber-village.net',
> @.priority = N'High',
> @.subject = N'headache',
> @.message = N'<HTML><H1>Hello SQL Server SMTP SQL
> Mail</H1></HTML>',
> @.type = N'text/html',
> @.attachments = N'',
> @.codepage = 0
> exec @.rc = master.dbo.xp_smtp_sendmail
> @.FROM = @.FROM,
> @.TO = @.TO,
> @.CC = @.CC,
> @.priority = @.priority,
> @.subject = @.subject,
> @.message = @.message,
> @.type = @.type,
> @.attachments = @.attachments,
> @.codepage = @.codepage,
> @.server = N'mail.cyber-village.net'
> select RC = @.rc
> end