Showing posts with label following. Show all posts
Showing posts with label following. Show all posts

Friday, March 30, 2012

Renaming databases with grace, any hint ?

Hi,

I want to implement the following pattern (pseudo-code follows):

[for index in x different databases, where x is rather big]

    create database MyDatabase_#{index}_Temp launch a dedicated ssis package on this database if I detect no error, proudly rename MyDatabase_#{index}_Temp to MyDatabase_#{index}_Last_Known_Good (drop the last_known_good first if it exist)


Later on, a consolidation process collect the data in Last_Known_Good versions of the databases.

My question: what is the simpliest way to achieve the rename operation (step 3) ?

I have tried an "alter database #{old_database_name} modify name = #{new_database_name}", but it works only once: once renamed, the database keeps the old mdf and ldf filenames, and the next create database will choke on this.

I have also tried to rename the logical filenames with ALTER DATABASE XXX MODIFY FILE (NAME = YYY, NEWNAME = ZZZ) but it doesn't work either.

I'd like to avoid specifying absolute mdf and ldf filenames myself, is it possible ?

best,

Thibaut

The best method for what you describe is probably to DETACH the "last good" and ATTACH the "new" as the old name. Yes, you will need to know the file names.
|||Thanks Tom. Too bad I have to know the file names, this pattern is very useful...

sql

Monday, March 26, 2012

Rename Measure caused error when process AS 2005 cube.

I just renamed one of my measures from 'POLICYCOUNT' to 'Policy Count'. I've got the following error when process the cube.

MdxScript(Production) (66, 24) The dimension '[POLICYCOUNT]' was not found in the cube when the string, [POLICYCOUNT], was parsed.

Any clues why I have this error? POLICYCOUNT is not a dimension.

Mitch

You are likely referencing the POLICYCOUNT measure in the calculation script. Open the Calculations tab in the cube editor and search for POLICYCOUNT.|||Also, you got an error message saying it is "dimension", because you used POLICYCOUNT unqualified inside MDX Script, which is a bad practice. You should use Measures.POLICYCOUNT instead, or, after the rename, Measures.[Policy Count].|||I had [Measures].[POLICYCOUNT] in my expression. After I changed it to [Measures].[Policy Count], it worked.sql

Friday, March 23, 2012

Rename database

I have set the database to single user mode and want to rename the database.
However, it shows the following message:
To change the NAME, the database must be in state in which a checkpoint can
be executed.
I have restart the sql server service but it still shows the above message.
What should I do?
Best Regards,
IvanHi Ivan
I get a different error if I am not connected to the single user database in
SQL 2005, but I can rename a database in single user mode if I am connected
to it or if no-one else is connected to it.
I assume you are using SQL 2000? Is anyone else connected to the database?
John
"Ivan" wrote:

> I have set the database to single user mode and want to rename the databas
e.
> However, it shows the following message:
> To change the NAME, the database must be in state in which a checkpoint ca
n
> be executed.
> I have restart the sql server service but it still shows the above message
.
> What should I do?
> Best Regards,
> Ivan
>
>|||In SQL2k with single user I can rename database easily.
Please check other settings also.
Regards
Amish Shah|||The database is in Single User mode and I have restart the SQL Server 2000
to ensure no one is connect to the database.
However, it still shows that error message.
Ivan
"amish" <shahamishm@.gmail.com>
'?:1147525165.494414.185770@.j33g2000cwa.googlegroups.com...
> In SQL2k with single user I can rename database easily.
> Please check other settings also.
> Regards
> Amish Shah
>|||Is the database READ_ONLY? It must be writable before it can be renamed.
Hope this helps.
Dan Guzman
SQL Server MVP
"Ivan" <ivan@.microsoft.com> wrote in message
news:eg3ONfrdGHA.3484@.TK2MSFTNGP04.phx.gbl...
> The database is in Single User mode and I have restart the SQL Server 2000
> to ensure no one is connect to the database.
> However, it still shows that error message.
> Ivan
> "amish" <shahamishm@.gmail.com>
> '?:1147525165.494414.185770@.j33g2000cwa.googlegroups.com...
>|||Hi
If you make the database the current database and then use
ALTER DATABASE MyDB SET SINGLE_USER WITH ROLLBACK IMMEDIATE
you should not need to reboot the server.
If you reboot the server and someone connects to the database before you
then you will have a problem.
John
"Ivan" wrote:

> The database is in Single User mode and I have restart the SQL Server 2000
> to ensure no one is connect to the database.
> However, it still shows that error message.
> Ivan
> "amish" <shahamishm@.gmail.com>
> '?:1147525165.494414.185770@.j33g2000cwa.googlegroups.com...
>
>sql

Rename database

I have set the database to single user mode and want to rename the database.
However, it shows the following message:
To change the NAME, the database must be in state in which a checkpoint can
be executed.
I have restart the sql server service but it still shows the above message.
What should I do?
Best Regards,
IvanHi Ivan
I get a different error if I am not connected to the single user database in
SQL 2005, but I can rename a database in single user mode if I am connected
to it or if no-one else is connected to it.
I assume you are using SQL 2000? Is anyone else connected to the database?
John
"Ivan" wrote:
> I have set the database to single user mode and want to rename the database.
> However, it shows the following message:
> To change the NAME, the database must be in state in which a checkpoint can
> be executed.
> I have restart the sql server service but it still shows the above message.
> What should I do?
> Best Regards,
> Ivan
>
>|||In SQL2k with single user I can rename database easily.
Please check other settings also.
Regards
Amish Shah|||The database is in Single User mode and I have restart the SQL Server 2000
to ensure no one is connect to the database.
However, it still shows that error message.
Ivan
"amish" <shahamishm@.gmail.com>
'?:1147525165.494414.185770@.j33g2000cwa.googlegroups.com...
> In SQL2k with single user I can rename database easily.
> Please check other settings also.
> Regards
> Amish Shah
>|||Is the database READ_ONLY? It must be writable before it can be renamed.
--
Hope this helps.
Dan Guzman
SQL Server MVP
"Ivan" <ivan@.microsoft.com> wrote in message
news:eg3ONfrdGHA.3484@.TK2MSFTNGP04.phx.gbl...
> The database is in Single User mode and I have restart the SQL Server 2000
> to ensure no one is connect to the database.
> However, it still shows that error message.
> Ivan
> "amish" <shahamishm@.gmail.com>
> '?:1147525165.494414.185770@.j33g2000cwa.googlegroups.com...
>> In SQL2k with single user I can rename database easily.
>> Please check other settings also.
>> Regards
>> Amish Shah
>|||Hi
If you make the database the current database and then use
ALTER DATABASE MyDB SET SINGLE_USER WITH ROLLBACK IMMEDIATE
you should not need to reboot the server.
If you reboot the server and someone connects to the database before you
then you will have a problem.
John
"Ivan" wrote:
> The database is in Single User mode and I have restart the SQL Server 2000
> to ensure no one is connect to the database.
> However, it still shows that error message.
> Ivan
> "amish" <shahamishm@.gmail.com>
> '?:1147525165.494414.185770@.j33g2000cwa.googlegroups.com...
> > In SQL2k with single user I can rename database easily.
> > Please check other settings also.
> >
> > Regards
> > Amish Shah
> >
>
>sql

Wednesday, March 21, 2012

Rename and Stored procedure

Hi All,
I have been working on the following problem for more than two ws
but without any luck.
We have ftp server on windows 2003 Exchange server with IIS 6.0. There
is a DSN to connect to SQL Server 2000 databases from the FTP site.
When a file is ftped, it populates the database with file name and the
group name. Stored procedures run every minute to check whether the
database has been populated with the file information. Then it tries to
rename the file with timestamp.
When I use the rename command in the query analyzer, it can rename the
file but from the stored procedure the same code is not successful in
renaming the file.
Here is the rename portion of the code in the stored procedure.
select @.renamestring = 'rename \\W2K3\data\' + rtrim(@.oldfilename) + '
'+ rtrim(@.newfilename)
declare @.result int
declare @.querystring char(200)
EXEC @.result = master.dbo.xp_cmdshell @.renamestring
here is the same in the QA to test , which renames really fine.
master..xp_cmdshell 'rename \\w2k3\data\ftp\test.txt test.sql'
I know it is hard to understand the problem looking at the portion of
the problem. But if you need I can follow-up with the complete code.
I will highly appreciate if you could help me. I have been working on
this for the last two ws.
I thank you in advance million times for your help.
Best regards,
mamunMay be there are files where the name include spaces, in these cases you hav
e
to enclose the path between double quote.
Example:
exec master..xp_cmdshell 'ren "\temp\test 1.txt" test1.txt'
go
AMB
"microsoft.public.dotnet.languages.vb" wrote:

> Hi All,
> I have been working on the following problem for more than two ws
> but without any luck.
> We have ftp server on windows 2003 Exchange server with IIS 6.0. There
> is a DSN to connect to SQL Server 2000 databases from the FTP site.
> When a file is ftped, it populates the database with file name and the
> group name. Stored procedures run every minute to check whether the
> database has been populated with the file information. Then it tries to
> rename the file with timestamp.
> When I use the rename command in the query analyzer, it can rename the
> file but from the stored procedure the same code is not successful in
> renaming the file.
> Here is the rename portion of the code in the stored procedure.
> select @.renamestring = 'rename \\W2K3\data' + rtrim(@.oldfilename) + '
> '+ rtrim(@.newfilename)
> declare @.result int
> declare @.querystring char(200)
> EXEC @.result = master.dbo.xp_cmdshell @.renamestring
>
> here is the same in the QA to test , which renames really fine.
> master..xp_cmdshell 'rename \\w2k3\data\ftp\test.txt test.sql'
>
> I know it is hard to understand the problem looking at the portion of
> the problem. But if you need I can follow-up with the complete code.
> I will highly appreciate if you could help me. I have been working on
> this for the last two ws.
> I thank you in advance million times for your help.
> Best regards,
> mamun
>|||HI AMB,
Here is my complete codes of the two sps. I am still without any luck.
Could anyone see any errors in the following code?
As always I am thankful for your help.
best regards,
mamun
SP1:
CREATE Procedure sp_FTPNotify_New (@.logId int)
As
declare @.recips varchar(255)
declare @.msg varchar(250)
declare @.sub varchar(75)
DECLARE @.cmd varchar(56)
Declare @.txtPtr varbinary(16)
declare @.new int, @.old int
declare @.txt varchar(255)
declare @.email varchar(50)
declare @.newfilename varchar(255)
declare @.oldfilename varchar(255)
declare @.renamestring varchar(255)
declare @.fieldposition int
declare @.NewTarget varchar(255)
declare @.newprefix varchar(20)
declare @.FailedFlag varchar(255)
declare @.LogTime datetime
Select @.NewTarget=Target , @.newprefix =
rtrim(rtrim(convert(char,logtime,12))+co
nvert(char,LogID)),
@.FailedFlag = Operation,@.LogTime = LogTime
from FTPLogs l , FTPNotify n
where LogID = @.logId and
lower(l.username) = lower(n.username)
select @.fieldposition=0
WHILE @.fieldposition < 225
BEGIN
select @.fieldposition = @.fieldposition+1
if substring(@.FailedFlag,@.fieldposition,1) = ']'
break
END
if lower(substring(@.FailedFlag,@.fieldpositi
on+1,255-@.fieldposition))
='created'
begin
select @.fieldposition=0
WHILE @.fieldposition < 225
BEGIN
select @.fieldposition = @.fieldposition+1
if substring(@.NewTarget,@.fieldposition,1) = '.'
break
END
Select
@.NewTarget=rTrim(substring(@.NewTarget,1,
@.fieldposition-1))+rtrim(@.newprefix+
rTrim(substring(@.NewTarget,@.fieldpositio
n,255)))
Select @.msg = 'This is an automatically generated FTP notification
message:'
delete from texttab
INSERT into texttab
select 'This message was generated on ' +
convert(varchar(25),getdate()) + char(13) + replicate ('_', 45) +
char(13)
select
@.recips = ' + rtrim(notify) + ',
@.txt =
'The file from ' + rtrim(description) + ' has arrived. This file is
located on the server W2K3-S1 ' +
'under the DATA' + rtrim(l.username) + ' directory.' + char(13) +
char(13) +
'The file name: ' + rtrim(@.NewTarget) + char(13) +
'Date Received: ' + convert(varchar(25),LogTime) + char(13) +
'File Size: ' + convert(char(20), BytesRecvd) ,
@.sub = 'FTPLog Notification from " + rtrim(description) + ',
@.newfilename = rtrim(@.NewTarget),
@.oldfilename = rtrim(l.username)+''+ rtrim(Target)
from FTPLogs l , FTPNotify n
where LogID = @.logId and
lower(l.username) = lower(n.username)
select @.txtptr = textptr(c1) from texttab
UPDATETEXT texttab.c1 @.txtptr NULL 0 with log @.txt
SELECT @.cmd = 'SELECT c1 FROM FTPLogs.dbo.texttab'
exec master.dbo.xp_sendmail
@.recipients = ' + @.recips + ',
@.message = ' + @.msg + ',
@.query = '+ @.cmd + ',
@.subject = ' + @.sub + ',
@.no_header = 'TRUE', @.width = 2500
delete from texttab
select @.renamestring = 'rename \\W2k3-S1\data' + rtrim(@.oldfilename) +
' '+ rtrim(@.newfilename)
print @.renamestring
declare @.result int
declare @.querystring char(200)
EXEC @.result = master.dbo.xp_cmdshell @.renamestring
if (@.result = 1)
begin
select @.querystring ='SELECT logid,substring(username,1,20)
username,logtime,bytesrecvd,substring(ta
rget,1,50) filename FROM
ftplogs.dbo.ftplogs where logid = '+ convert(char,@.logId)
exec master.dbo.xp_sendmail @.recipients = 'mamun@.inc.com',
@.query = '" + @.querystring + " ' ,
@.subject ='Failed Rename',
@.message ='The following file could not be renamed.',
@.attach_results = 'FALSE', @.width = 250
end
end
Else
if lower(substring(@.FailedFlag,@.fieldpositi
on+1,255-@.fieldposition)) =
'closed'
begin
select @.fieldposition=0
WHILE @.fieldposition < 225
BEGIN
select @.fieldposition = @.fieldposition+1
if substring(@.NewTarget,@.fieldposition,1) = '.'
break
END
Select
@.NewTarget=rTrim(substring(@.NewTarget,1,
@.fieldposition-1))+rtrim(@.newprefix+
rTrim(substring(@.NewTarget,@.fieldpositio
n,255)))
Select @.msg = 'This is an automatically generated FTP notification
message:'
delete from texttab
INSERT into texttab
select 'This message was generated on ' +
convert(varchar(25),getdate()) + char(13) +
replicate ('_', 45) + char(13)
select
@.recips = ''' + rtrim(notify) + ''',
@.txt = char(13)+
'THE ATEMPTED FTP FILE TRANSFER TO SERVICES '+ char(13)
+'ON ' +UPPER(convert(varchar(25),LOGTIME)) + ' FROM ' +
upper(rtrim(description)) + char(13)
+'WAS NOT SUCCESSFULLY RECEIVED.'+ char(13)+ char(13)
+'IF NECESSARY PLEASE CONTACT THE APPROPRIATE PARTY' + char(13)
+'TO HAVE THE FILE RESENT.' ,
--@.sub = 'FTPLog Notification ALERT from ' + rtrim(description) + '''
@.sub = 'FTPLog Notification ALERT from ' + rtrim(description) + ''
from FTPLogs l , FTPNotify n
where LogID = @.logId and
lower(l.username) = lower(n.username)
select @.txtptr = textptr(c1) from texttab
UPDATETEXT texttab.c1 @.txtptr NULL 0 with log @.txt
SELECT @.cmd = 'SELECT c1 FROM FTPLogs.dbo.texttab'
exec master.dbo.xp_sendmail
@.recipients = " + @.recips + ",
@.message = " + @.msg + ",
@.query = "+ @.cmd + ",
@.subject = " + @.sub + ",
@.no_header = 'TRUE', @.width = 2500
delete from texttab
end
GO
SP2:
CREATE Procedure sp_MailNotify_New
As
Declare @.id int
Declare @.CStatus int
Declare @.fieldposition int
declare @.FailedFlag varchar(255)
Declare C_getLog cursor for
select LogId,Operation from FTPLogs where notified = 0 order by LogID
Open C_getLog
Fetch Next from C_getLog into @.id,@.FailedFlag
select @.CStatus = @.@.FETCH_STATUS
select @.fieldposition=0
WHILE @.fieldposition < 225
BEGIN
select @.fieldposition = @.fieldposition+1
if substring(@.FailedFlag,@.fieldposition,1) = ']'
break
END
select @.FailedFlag =
lower(substring(@.FailedFlag,@.fieldpositi
on+1,255-@.fieldposition))
while (@.CStatus <> -1 and (@.FailedFlag = 'closed' or @.FailedFlag =
'created'))
begin
select 'THE ID is ' + convert(char(8),@.id)
execute sp_FTPNotify_new @.id
update FTPLogs set notified = 1 where LogId = @.id
Fetch Next from C_getLog into @.id,@.FailedFlag
select @.CStatus = @.@.FETCH_STATUS
select @.fieldposition=0
WHILE @.fieldposition < 225
BEGIN
select @.fieldposition = @.fieldposition+1
if substring(@.FailedFlag,@.fieldposition,1) = ']'
break
END
select @.FailedFlag =
lower(substring(@.FailedFlag,@.fieldpositi
on+1,255-@.fieldposition))
end
update FTPLogs set notified = 1 where LogId <= @.id
Close C_getLog
Deallocate C_getLog
GO
In the scheduled jobs: exec exec sp_MailNotify_New

Tuesday, March 20, 2012

removing windows group that's a database user

When attempting to remove a windows group using the
stored procedure xp_dropuser obtain the following error:
Server: Msg 15008, Level 16, State 1, Procedure
sp_dropuser, Line 12
User 'D0550000\CSMBalanceInquiry' does not exist in the
current database.
D0550000 is the domain and CSMBalanceInquiry is the
windows group.
Before removing this group I execute sp_helpuser and do a
copy and paste of the name.
If I attempt to remove this group through the enterprise
manager, I do not have a problem. I tried to search in
the Microsoft knowledge base and could not find an
article explaining this problem. Thank you for your help
in advance.You need to specify the value returned from sp_helpuser for the "name" of
the user\group.
You can see the valid names in the database by running select * from
sysusers while in that database.
Thanks,
Kevin McDonnell
Microsoft Corporation
This posting is provided AS IS with no warranties, and confers no rights.|||is the group a role or a user? Note that sp_helpuser can return information
about roles as well as users...
If you think that might be the case try sp_droprole.
Richard Waymire, MCSE, MCDBA
This posting is provided "AS IS" with no warranties, and confers no rights.
"Maria Garcia" <garcim@.miamidade.gov> wrote in message
news:009e01c3c3fe$8e7b9880$a401280a@.phx.gbl...
quote:

> When attempting to remove a windows group using the
> stored procedure xp_dropuser obtain the following error:
> Server: Msg 15008, Level 16, State 1, Procedure
> sp_dropuser, Line 12
> User 'D0550000\CSMBalanceInquiry' does not exist in the
> current database.
> D0550000 is the domain and CSMBalanceInquiry is the
> windows group.
> Before removing this group I execute sp_helpuser and do a
> copy and paste of the name.
> If I attempt to remove this group through the enterprise
> manager, I do not have a problem. I tried to search in
> the Microsoft knowledge base and could not find an
> article explaining this problem. Thank you for your help
> in advance.

Monday, March 12, 2012

Removing SQL Express instances

Using SQL Express, UNDER COMPUTER management, SQL Server Configuraqtion Manager, SQL Server 2005 Network configuration Ihave the following listed instances

CSOLUTION - not listed under services

MSSQLSERVER - Disabled

SQLEXPRESS - Diaabled

SQLTEST - Disabled

SQLEXPRESSDATA - used by VB05 program

Questions

a) how do I delete all the instancces I do not use. In MSDE, each instance could be removed via the add/remove programs, but I don't see how to remove it under express

b) since SQLEXPRESS is the default for an SQL Express installation, I would like for the VBB05 program to use that instance; how do I do that?

Any help would be appreciated.

Bob

a) When you go to Add/Remove Programs, select the SQL 2005 entry and click on Remove. This will pop up a box showing the instances you have on your machine. You can select one from here and continue with the uninstall to remove it.

b) When you create your DB connection in your VB app, you'll need to supply a DB name to connect to. Here, use <MACHINENAME>/SQLEXPRESS and it will connect to that instance. You'd do the same thing to connect to any other named instance.

Thanks,
Sam Lester (MSFT)

Removing SQL Express instances

Using SQL Express, UNDER COMPUTER management, SQL Server Configuraqtion Manager, SQL Server 2005 Network configuration Ihave the following listed instances

CSOLUTION - not listed under services

MSSQLSERVER - Disabled

SQLEXPRESS - Diaabled

SQLTEST - Disabled

SQLEXPRESSDATA - used by VB05 program

Questions

a) how do I delete all the instancces I do not use. In MSDE, each instance could be removed via the add/remove programs, but I don't see how to remove it under express

b) since SQLEXPRESS is the default for an SQL Express installation, I would like for the VBB05 program to use that instance; how do I do that?

Any help would be appreciated.

Bob

a) When you go to Add/Remove Programs, select the SQL 2005 entry and click on Remove. This will pop up a box showing the instances you have on your machine. You can select one from here and continue with the uninstall to remove it.

b) When you create your DB connection in your VB app, you'll need to supply a DB name to connect to. Here, use <MACHINENAME>/SQLEXPRESS and it will connect to that instance. You'd do the same thing to connect to any other named instance.

Thanks,
Sam Lester (MSFT)

Removing rows from a result

I have a query which returns the following results. What i want to do
is remove any rows where col1 does not appear in col2. e.g. "107". As
well as this i want to remove any rows that are affected by this, e.g.
"109" as once 107 goes, 109 does not appear in col2.
Thanks in advance.
col1 col2
1 108
1 112
1 115
1 116
1 117
1 118
107 109
108 114
109 110
110 111
112 113
118 119On Apr 3, 3:45 pm, danielev...@.gmail.com wrote:
> I have a query which returns the following results. What i want to do
> is remove any rows where col1 does not appear in col2. e.g. "107". As
> well as this i want to remove any rows that are affected by this, e.g.
> "109" as once 107 goes, 109 does not appear in col2.
> Thanks in advance.
> col1 col2
> 1 108
> 1 112
> 1 115
> 1 116
> 1 117
> 1 118
> 107 109
> 108 114
> 109 110
> 110 111
> 112 113
> 118 119
Try something like this
drop table #temp
drop table #temp1
create table #temp (col1 int, col2 int )
insert into #temp values (1, 108 )
insert into #temp values (1, 112 )
insert into #temp values (1, 115 )
insert into #temp values (1, 116 )
insert into #temp values (1, 117 )
insert into #temp values (1 ,118 )
insert into #temp values (107,109 )
insert into #temp values (108,114 )
insert into #temp values (109,110 )
insert into #temp values (110,111 )
insert into #temp values (112,113 )
insert into #temp values (118,119 )
insert into #temp values (118,1 )
create table #temp1 (col1 int, col2 int )
while 1 = 1
begin
truncate table #temp1
insert into #temp1 select * from #temp a where not exists (select 1
from #temp b where a.col1 = b.col2 )
if @.@.rowcount = 0
break
delete #temp from #temp
inner join #temp1 on #temp.col1 = #temp1.col1
end
select * from #temp|||On Apr 3, 12:59 pm, "M A Srinivas" <masri...@.gmail.com> wrote:
> On Apr 3, 3:45 pm, danielev...@.gmail.com wrote:
>
> > I have a query which returns the following results. What i want to do
> > is remove any rows where col1 does not appear in col2. e.g. "107". As
> > well as this i want to remove any rows that are affected by this, e.g.
> > "109" as once 107 goes, 109 does not appear in col2.
> > Thanks in advance.
> > col1 col2
> > 1 108
> > 1 112
> > 1 115
> > 1 116
> > 1 117
> > 1 118
> > 107 109
> > 108 114
> > 109 110
> > 110 111
> > 112 113
> > 118 119
> Try something like this
> drop table #temp
> drop table #temp1
> create table #temp (col1 int, col2 int )
> insert into #temp values (1, 108 )
> insert into #temp values (1, 112 )
> insert into #temp values (1, 115 )
> insert into #temp values (1, 116 )
> insert into #temp values (1, 117 )
> insert into #temp values (1 ,118 )
> insert into #temp values (107,109 )
> insert into #temp values (108,114 )
> insert into #temp values (109,110 )
> insert into #temp values (110,111 )
> insert into #temp values (112,113 )
> insert into #temp values (118,119 )
> insert into #temp values (118,1 )
> create table #temp1 (col1 int, col2 int )
> while 1 = 1
> begin
> truncate table #temp1
> insert into #temp1 select * from #temp a where not exists (select 1
> from #temp b where a.col1 = b.col2 )
> if @.@.rowcount = 0
> break
> delete #temp from #temp
> inner join #temp1 on #temp.col1 = #temp1.col1
> end
> select * from #temp
Thanks for this. Is this the only way to achieve this result? I was
hoping to do it in the initial query without using temp tables.
Dan

Removing rows from a result

I have a query which returns the following results. What i want to do
is remove any rows where col1 does not appear in col2. e.g. "107". As
well as this i want to remove any rows that are affected by this, e.g.
"109" as once 107 goes, 109 does not appear in col2.
Thanks in advance.
col1 col2
1108
1112
1115
1116
1117
1118
107109
108114
109110
110111
112113
118119
On Apr 3, 3:45 pm, danielev...@.gmail.com wrote:
> I have a query which returns the following results. What i want to do
> is remove any rows where col1 does not appear in col2. e.g. "107". As
> well as this i want to remove any rows that are affected by this, e.g.
> "109" as once 107 goes, 109 does not appear in col2.
> Thanks in advance.
> col1 col2
> 1 108
> 1 112
> 1 115
> 1 116
> 1 117
> 1 118
> 107 109
> 108 114
> 109 110
> 110 111
> 112 113
> 118 119
Try something like this
drop table #temp
drop table #temp1
create table #temp (col1 int, col2 int )
insert into #temp values (1, 108 )
insert into #temp values (1, 112 )
insert into #temp values (1, 115 )
insert into #temp values (1, 116 )
insert into #temp values (1, 117 )
insert into #temp values (1 ,118 )
insert into #temp values (107,109 )
insert into #temp values (108,114 )
insert into #temp values (109,110 )
insert into #temp values (110,111 )
insert into #temp values (112,113 )
insert into #temp values (118,119 )
insert into #temp values (118,1 )
create table #temp1 (col1 int, col2 int )
while 1 = 1
begin
truncate table #temp1
insert into #temp1 select * from #temp a where not exists (select 1
from #temp b where a.col1 = b.col2 )
if @.@.rowcount = 0
break
delete #temp from #temp
inner join #temp1 on #temp.col1 = #temp1.col1
end
select * from #temp
|||On Apr 3, 12:59 pm, "M A Srinivas" <masri...@.gmail.com> wrote:
> On Apr 3, 3:45 pm, danielev...@.gmail.com wrote:
>
>
>
> Try something like this
> drop table #temp
> drop table #temp1
> create table #temp (col1 int, col2 int )
> insert into #temp values (1, 108 )
> insert into #temp values (1, 112 )
> insert into #temp values (1, 115 )
> insert into #temp values (1, 116 )
> insert into #temp values (1, 117 )
> insert into #temp values (1 ,118 )
> insert into #temp values (107,109 )
> insert into #temp values (108,114 )
> insert into #temp values (109,110 )
> insert into #temp values (110,111 )
> insert into #temp values (112,113 )
> insert into #temp values (118,119 )
> insert into #temp values (118,1 )
> create table #temp1 (col1 int, col2 int )
> while 1 = 1
> begin
> truncate table #temp1
> insert into #temp1 select * from #temp a where not exists (select 1
> from #temp b where a.col1 = b.col2 )
> if @.@.rowcount = 0
> break
> delete #temp from #temp
> inner join #temp1 on #temp.col1 = #temp1.col1
> end
> select * from #temp
Thanks for this. Is this the only way to achieve this result? I was
hoping to do it in the initial query without using temp tables.
Dan

Removing rows from a result

I have a query which returns the following results. What i want to do
is remove any rows where col1 does not appear in col2. e.g. "107". As
well as this i want to remove any rows that are affected by this, e.g.
"109" as once 107 goes, 109 does not appear in col2.
Thanks in advance.
col1 col2
1 108
1 112
1 115
1 116
1 117
1 118
107 109
108 114
109 110
110 111
112 113
118 119On Apr 3, 3:45 pm, danielev...@.gmail.com wrote:
> I have a query which returns the following results. What i want to do
> is remove any rows where col1 does not appear in col2. e.g. "107". As
> well as this i want to remove any rows that are affected by this, e.g.
> "109" as once 107 goes, 109 does not appear in col2.
> Thanks in advance.
> col1 col2
> 1 108
> 1 112
> 1 115
> 1 116
> 1 117
> 1 118
> 107 109
> 108 114
> 109 110
> 110 111
> 112 113
> 118 119
Try something like this
drop table #temp
drop table #temp1
create table #temp (col1 int, col2 int )
insert into #temp values (1, 108 )
insert into #temp values (1, 112 )
insert into #temp values (1, 115 )
insert into #temp values (1, 116 )
insert into #temp values (1, 117 )
insert into #temp values (1 ,118 )
insert into #temp values (107,109 )
insert into #temp values (108,114 )
insert into #temp values (109,110 )
insert into #temp values (110,111 )
insert into #temp values (112,113 )
insert into #temp values (118,119 )
insert into #temp values (118,1 )
create table #temp1 (col1 int, col2 int )
while 1 = 1
begin
truncate table #temp1
insert into #temp1 select * from #temp a where not exists (select 1
from #temp b where a.col1 = b.col2 )
if @.@.rowcount = 0
break
delete #temp from #temp
inner join #temp1 on #temp.col1 = #temp1.col1
end
select * from #temp|||On Apr 3, 12:59 pm, "M A Srinivas" <masri...@.gmail.com> wrote:
> On Apr 3, 3:45 pm, danielev...@.gmail.com wrote:
>
>
>
>
> Try something like this
> drop table #temp
> drop table #temp1
> create table #temp (col1 int, col2 int )
> insert into #temp values (1, 108 )
> insert into #temp values (1, 112 )
> insert into #temp values (1, 115 )
> insert into #temp values (1, 116 )
> insert into #temp values (1, 117 )
> insert into #temp values (1 ,118 )
> insert into #temp values (107,109 )
> insert into #temp values (108,114 )
> insert into #temp values (109,110 )
> insert into #temp values (110,111 )
> insert into #temp values (112,113 )
> insert into #temp values (118,119 )
> insert into #temp values (118,1 )
> create table #temp1 (col1 int, col2 int )
> while 1 = 1
> begin
> truncate table #temp1
> insert into #temp1 select * from #temp a where not exists (select
1
> from #temp b where a.col1 = b.col2 )
> if @.@.rowcount = 0
> break
> delete #temp from #temp
> inner join #temp1 on #temp.col1 = #temp1.col1
> end
> select * from #temp
Thanks for this. Is this the only way to achieve this result? I was
hoping to do it in the initial query without using temp tables.
Dan

Friday, March 9, 2012

Removing null values from a result set

Hi,

I have following query which is returing null values along with other values. Could you please tell me how I can restrict to not to return null values

SELECT [Measures].[Total Hours] ON 0,
[Employee].[Hierarchy].[Employee Key]ON 1
FROM[Labor Metrics]

Thanks,

Prudhvi

Prud,

I'm assuming that you want to filter out TotalHours

SELECT [Measures].[Total Hours] ON 0,
[Employee].[Hierarchy].[Employee Key]ON 1
FROM[Labor Metrics]

Where [Measures].[Total Hours] is not null

Ham

|||

Thanks Ham. I did tried this earlier but got the following error message

Executing the query ...
Query (5, 1) The IS function expects a member expression for the 2 argument. A string or numeric expression was used.
Execution complete

|||

Prud,

Are you using SQL 2000 or SQL 2005? The "is not null" is a valid for SQL in the Where clause.

Ham

|||

SSAS 2005 introduces a HAVING clause:

SELECT [Measures].[Total Hours] ON 0,
[Employee].[Hierarchy].[Employee Key]

HAVING [Measures].[Total Hours] <> 0 ON 1
FROM[Labor Metrics]

Or, you can use Filter().

|||

Thanks Teo, it worked.

Wednesday, March 7, 2012

Removing Duplicates.

Here is the problem im trying to show Date and time for my selection, when i use the following code it shows the Date and time but leaves dups

SELECT DISTINCT(Item), OutDateTime_dt FROM Database Where AFTID_int = 2149 AND CID_int = 0 AND MID_int = 0 AND Item <> '' AND OutDateTime_dt >= '09/17/2003' AND OutDateTime_dt < '9/24/2003' Order by OutDateTime_dt Desc

However, if i remove the "OutDateTime_dt" from the first line,
leaving this

SELECT DISTINCT(Item) FROM Database

It removes the duplicates and works fine but doesnt give me the time as i need as well or date. Please help.If you only want one row per Item, and each Item may have many OutDateTime values, then you would have to choose which OutDateTime to display, e.g. the highest:

SELECT Item, MAX(OutDateTime_dt) FROM Database Where AFTID_int = 2149 AND CID_int = 0 AND MID_int = 0 AND Item <> '' AND OutDateTime_dt >= '09/17/2003' AND OutDateTime_dt < '9/24/2003' GROUP BY Item;

Removing duplicates

Hi

I have inherited a web app with the following table structure, and need to
produce a table without any duplicates. Email seems like the best unique
identifier - so only one of each e-mail address should be in the table.

Following http://www.sqlteam.com/item.asp?ItemID=3331 I have been able to
get a duplicate count working:

select Email, count(*) as UserCount
from dbo.Members
group by Email
having count(*) > 1
order by UserCount desc

But the methods for create a new table without duplicates fail. My code for
the 2nd method is:

sp_rename 'Members', 'temp_Members'

select distinct *
into Members
from temp_Members

Table...

CREATE TABLE [dbo].[Members] (
[MemberID] [int] IDENTITY (1, 1) NOT NULL ,
[Username] [varchar] (10) COLLATE Latin1_General_CI_AS NOT NULL ,
[Password] [varchar] (10) COLLATE Latin1_General_CI_AS NOT NULL ,
[Email] [varchar] (50) COLLATE Latin1_General_CI_AS NOT NULL ,
[Title] [varchar] (10) COLLATE Latin1_General_CI_AS NOT NULL ,
[FirstName] [varchar] (50) COLLATE Latin1_General_CI_AS NOT NULL ,
[Surname] [varchar] (50) COLLATE Latin1_General_CI_AS NOT NULL ,
[Address1] [varchar] (35) COLLATE Latin1_General_CI_AS NOT NULL ,
[Address2] [varchar] (35) COLLATE Latin1_General_CI_AS NOT NULL ,
[City] [varchar] (25) COLLATE Latin1_General_CI_AS NOT NULL ,
[Country] [varchar] (25) COLLATE Latin1_General_CI_AS NOT NULL ,
[Profession] [varchar] (50) COLLATE Latin1_General_CI_AS NOT NULL ,
[Publication] [varchar] (40) COLLATE Latin1_General_CI_AS NOT NULL ,
[DateAdded] [smalldatetime] NOT NULL ,
[SendMail] [smallint] NOT NULL
) ON [PRIMARY]
GO

Thanks B.Can I assume that memberid is unique? If so, try this:

INSERT INTO NewTable
(username, password, email, title, firstname, surname, address1,
address2, city, country, profession, publication, dateadded, sendmail)
SELECT username, password, email, title, firstname, surname, address1,
address2, city, country, profession, publication, dateadded, sendmail
FROM Members AS M1
JOIN
(SELECT MIN(memberid) AS memberid
FROM Members
GROUP BY email) AS M2
ON M1.memberid = M2.memberid

--
David Portas
----
Please reply only to the newsgroup
--|||Microsoft has a great article on this at
http://support.microsoft.com/defaul...4&Product=sql2k

I use a code block for doing this that I load as required, I only run
it when I have to. Once you've cleaned up your data, put a unique
constraint to prevent dupes from getting back in.

This is the code that I use for cleaning up dupes, you'll have to
modify the table and field names obviously. This config assumes you
are allowing Select/Into's.

/*
--
--WARNING! EXECUTE THIS SCRIPT WITH EXTREME CAUTION!,
--AND THEN ONLY ONE STEP AT A TIME!
--
--In the TotalTransit (DDS) database, this needs to run against
--ddsTrip and ttVoucher_Trip. Since the two tables have different
--layouts, the table that holds dupe rows must be dropped and
--recreated every time this process is run.
--
--There are now two seperate scripts, one for each table.
--
--Run the process one step at a time by highlighting each step.
--
--Wayne West, 10/13/03
-- WW 10/17/03 -- split into two procs, added more comments
--
------------------------
--
--this is here to help find if there are any dupes
select tripid, count(*)
from ddsTrip
group by tripid
having count(*) > 1

select tripid, count(*)
from ttVoucher_Trip
group by tripid
having count(*) > 1
--
------------------------
*/
/*
--Step 1 -- Verify there are dupes in the table
select tripid, count(*)
from ddsTrip
group by tripid
having count(*) > 1

--Step 2a -- Drop table that will hold dupe key values
drop table zzzholdkey

--Step 2b -- Collect key values, save in zzzHoldKey
select RecKey = tripid, KeyCount = count(*)
into zzzHoldKey
from ddsTrip
group by tripid
having count(*) > 1

--Step 3a -- Drop table that will hold one instance of duplicate rows
drop table zzzholddupes

--Step 3b -- Collect one instance of duplicate rows
select DISTINCT t.*
into zzzHoldDupes
from ddsTrip t, zzzholdkey hld
where t.tripid = hld.reckey

--Step 4 -- See if more than one field in addition to TripID is
duplicated
--This will indicate additional steps must be taken to clean up the
table.
select count(*), tripid
from zzzholddupes
group by tripid
having count(*) > 1

--Step 5 -- Delete ALL rows based on key value of duplicate records
delete ddsTrip
from ddsTrip t, zzzholdkey hld
where t.tripid = hld.reckey

--Step 6 -- Reinsert the row captured in Step 3b
insert ddsTrip
select *
from zzzholddupes

--You probably ought to rerun Step 1 to make sure the file is clean.
*/

Saturday, February 25, 2012

Removing duplicate Records

I have a table that holds the following

1 7530568 87143 OESCHD 1/5/2006 6:31:58 AM
1 7530568 87143 OESCHD 1/5/2006 7:02:36 AM

for each 7530568 ordernumber there should only be one OESCHD status.

This is the query I'm using to insert the data sent to me.

INSERT INTO ORDER_EVENTS
SELECT d.division as division,
dt.orderNum as orderNum,
dt.poNum as poNum,
dt.statusCode as statusCode,
dt.statusChangeDate as statusChangeDate
FROM dt_Order_Events dt INNER JOIN
division d ON dt.division = d.divisionShort INNER JOIN
status s ON s.division = d.division AND s.statCode = dt.statusCode
WHERE directive <> 'C' AND
dt.orderNum IN (SELECT orderNum FROM ORDER_HEADER)

This works fine when used with in the hourly transactional update. But When I ran it for the Bulk UpDate (so we'd have historical data) it allowed orders to have statuses to many times.

I am not a SQL guru, I have no idea how to write a sql statement or stored proc that will remove the duplicate records. or how to change what I have to prevent further ones.

Any help would be apreciated.

look here:

http://www.sql-server-performance.com/dv_delete_duplicates.asp

you will get your answers there.

tomer

removing data

I have a table that I need to delete some data from and put the deleted
data into a different table.

How do I script the following.

If Field1 in Table1 is null, remove that row from Table1 and put it in
a new table called Table2

Regards,
CiarnCreate a newTable having the structure of Oldtable and write a query

Insert into newTable select * from OldTable where Field1 is null
delete from OldTable where Field1 is null

You can also use Trigger

Madhivanan|||Madhivanan wrote:
> Create a newTable having the structure of Oldtable and write a query

What is the quickest way to create a newtable with the structure of the
Oldtable? The Oldtable has over 50 fields, of many different types.|||chudson...@.hotmail.com wrote:
> Madhivanan wrote:
> > Create a newTable having the structure of Oldtable and write a
query
> What is the quickest way to create a newtable with the structure of
the
> Oldtable? The Oldtable has over 50 fields, of many different types.

Look in BOL under "generating scripts"

Edward
--
The reading group's reading group:
http://www.bookgroup.org.uk|||The most performant (as it hardly touches the transaction log) and
easiest way to copy the data into a new table is to use the SELECT INTO
command. Loom in Books Online for more detailed information on this
command and why it is best for this kind of operation.

This will dynamically create a new table for you with all of the
required columns and their basic attributes.

You can then delete the copied records from the source table.

Regards,

Malcolm
www.dbghost.com|||The quickest way is to highlight the table in Query Analyzer and right
click create script.

Removing Characters in Field grouping

Hello,

I am using the following expression to strip the last 11 characters in a field group and it returns an error saying that Len cannot use a negative number. It must be 0 or greater. Is there a better alternative?

Code Snippet

left(Fields!TestName.Value,len(Fields!TestName.Value)-11))

you can use Rtrim|||

Are you always removing 11 character regardless of the field length or do you simply want to restrict it to a certain size?

For example, the following code restricts the length to 5 characters:

=iif(len(rtrim(Fields!TestDATA.Value)) > 5, mid(rtrim(Fields!TestDATA.Value),1,5), rtrim(Fields!TESTDATA.Value))

Removing character from varchar

Hello all,
How would you remove the last character from the following string:
jmy0084t?
Thanks
JonJon wrote:
> Hello all,
> How would you remove the last character from the following string:
> jmy0084t?
> Thanks
> Jon
>
select left('jmy0084t',len('jmy0084t')-1)|||Hi
Such as
DECLARE @.str varchar(10)
SET @.str = 'jmy0084t?'
SELECT @.str,LEFT(@.str,LEN(@.str)-1),STUFF(@.str,LEN(@.str),1,'')
John
"Jon" wrote:
> Hello all,
> How would you remove the last character from the following string:
> jmy0084t?
> Thanks
> Jon
>