Tuesday, March 27, 2012
Comparing similarities in 2 tables - sql query
further:
I have 2 tables (one table for january records and 1 table for june records)
I need to find every instance where a drug name and quantity dispensed are
the exact same in both tables
Then I need to loop through (or can it be done in the query?) each instance
and display the RX numbers along with the drug names and quantities from each
table where that happens (rx from the january table will be different from
the rx in the june table)
(drug_name,quantity_dispensed and rx is a field which resides in both tables)
something like this:
drug quantity janrx junerx
-- -- -- --
aspirin 15 43323 8879676
aspirin 15 2322 12313
codeine 10 1222 989898
I probably did not make that clear but I will be glad to re-word it if
anyone is available to help me.
Thank-You
John
John
Don't work on sunday:-)))))))
create table #june
(
[id] int not null primary key,
drug varchar(50) not null,
qt int
)
insert into #june values (1,'aspirin',15)
insert into #june values (2,'codein',20)
insert into #june values (3,'codein',40)
insert into #june values (4,'optalgin',100)
create table #jan
(
[id] int not null primary key,
drug varchar(50) not null,
qt int
)
insert into #jan values (1,'aspirin',15)
insert into #jan values (2,'codein',20)
insert into #jan values (3,'codein',10)
insert into #jan values (4,'junk',100)
select j.drug,j.qt from #june j join #jan
on j.drug=#jan.drug and j.qt=#jan.qt
"John Jasper" <JohnJasper@.discussions.microsoft.com> wrote in message
news:5E1DF03B-88E7-4C5C-9D76-F162DCDA1013@.microsoft.com...
> hi, I have been working on this for about 3 hours now and I can't get any
> further:
> I have 2 tables (one table for january records and 1 table for june
> records)
> I need to find every instance where a drug name and quantity dispensed are
> the exact same in both tables
> Then I need to loop through (or can it be done in the query?) each
> instance
> and display the RX numbers along with the drug names and quantities from
> each
> table where that happens (rx from the january table will be different from
> the rx in the june table)
> (drug_name,quantity_dispensed and rx is a field which resides in both
> tables)
> something like this:
> drug quantity janrx junerx
> -- -- -- --
> aspirin 15 43323 8879676
> aspirin 15 2322 12313
> codeine 10 1222 989898
> I probably did not make that clear but I will be glad to re-word it if
> anyone is available to help me.
>
> Thank-You
> John
>
|||Thanks for such a quick reply !
I don't understand where those tables are created - they don't show up in
the Enterprise manager and when I run the query after i run those create
table queries - it says: invalid object #jan
invalid object #june
"Uri Dimant" wrote:
> John
> Don't work on sunday:-)))))))
>
> create table #june
> (
> [id] int not null primary key,
> drug varchar(50) not null,
> qt int
> )
> insert into #june values (1,'aspirin',15)
> insert into #june values (2,'codein',20)
> insert into #june values (3,'codein',40)
> insert into #june values (4,'optalgin',100)
>
> create table #jan
> (
> [id] int not null primary key,
> drug varchar(50) not null,
> qt int
> )
> insert into #jan values (1,'aspirin',15)
> insert into #jan values (2,'codein',20)
> insert into #jan values (3,'codein',10)
> insert into #jan values (4,'junk',100)
>
> --
> select j.drug,j.qt from #june j join #jan
> on j.drug=#jan.drug and j.qt=#jan.qt
>
>
> "John Jasper" <JohnJasper@.discussions.microsoft.com> wrote in message
> news:5E1DF03B-88E7-4C5C-9D76-F162DCDA1013@.microsoft.com...
>
>
|||John
Those tables are temporary tables which are created in tempdb database. I
did it just for testing.
The matter is does it work or it doesn't (i meant the solution)?
"John Jasper" <JohnJasper@.discussions.microsoft.com> wrote in message
news:E67FB1FB-5092-4CA2-B3C3-C59C080910EB@.microsoft.com...[vbcol=seagreen]
> Thanks for such a quick reply !
> I don't understand where those tables are created - they don't show up in
> the Enterprise manager and when I run the query after i run those create
> table queries - it says: invalid object #jan
> invalid object #june
> "Uri Dimant" wrote:
Sunday, March 11, 2012
compare results of 2 subqueries
subquery1 IN subquery2
i cant use the IN unless subquery1 returns a single value i want to compare multiple results like for example:
('name1','name2') in ('name1','name2','name3') returns true
how can i acheive this?
thanks--Something like this
select
Result=
case when not exists(
select *
from pubs.dbo.titleauthor titleauthor
where authors.au_id=titleauthor.au_id
)
then 'false'
else 'true'
end
,*
from pubs.dbo.authors authors|||thanks for the reply
i want to know if a set of words which i get from my app are included in the list of words i get from the database.
do u suggest i insert these words in a temp table then use the query as u suggested or is there another way where i dont have to create a table.
thank you|||You can select from everything, but it would be much simpler, if table is in database.|||Thank you
since am building the sql query from my app it easier to select the values i have as they are without adding a new table.
Tuesday, February 14, 2012
Command USE with variable
Why I can′t use the command USE with variable?
declare @.banco varchar(20)
select @.banco='xpto'
USE @.banco
I have try in other way without success
DECLARE @.banco varchar(50)
declare @.SQL varchar(50)
select @.banco='xpto'
print @.banco
select @.SQL='USE '+@.banco
print @.SQL
exec(@.SQL)
Help me, please.
Rodrigo
It is executed at both compile time and run time, so I presume that is the reason. Not all commands can use variables in place of constants. the TOP command, for instance, only takes constants until it was altered in SQL 2005. For USE sysntax and description:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_tsqlcon_6lyk.asp
|||I am developing a routine to desfragment
the indexes of all tables of the server... I′ve found one in the books online (DBCC SHOWCONTIG session), but I ′m trying to convert it to defrag all objetcs in all databases of my instance. So I have think in a "Cursor" that get the name of the databases in the "sysdatabases" system table. Than set each database to current and run the script. Fallow the script:
For this, I have think in set the database to run the script on the database context.
Can you help me?
/*Perform a 'USE <database name>' to select the database in which to run the script.*/ -- Declare variables SET NOCOUNT ON DECLARE @.tablename VARCHAR (128) DECLARE @.execstr VARCHAR (255) DECLARE @.objectid INT DECLARE @.indexid INT DECLARE @.frag DECIMAL DECLARE @.maxfrag DECIMAL -- Decide on the maximum fragmentation to allow SELECT @.maxfrag = 30.0 -- Declare cursor DECLARE tables CURSOR FOR SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' -- Create the table CREATE TABLE #fraglist ( ObjectName CHAR (255), ObjectId INT, IndexName CHAR (255), IndexId INT, Lvl INT, CountPages INT, CountRows INT, MinRecSize INT, MaxRecSize INT, AvgRecSize INT, ForRecCount INT, Extents INT, ExtentSwitches INT, AvgFreeBytes INT, AvgPageDensity INT, ScanDensity DECIMAL, BestCount INT, ActualCount INT, LogicalFrag DECIMAL, ExtentFrag DECIMAL) -- Open the cursor OPEN tables -- Loop through all the tables in the database FETCH NEXT FROM tables INTO @.tablename WHILE @.@.FETCH_STATUS = 0 BEGIN -- Do the showcontig of all indexes of the table INSERT INTO #fraglist EXEC ('DBCC SHOWCONTIG (''' + @.tablename + ''') WITH FAST, TABLERESULTS, ALL_INDEXES, NO_INFOMSGS') FETCH NEXT FROM tables INTO @.tablename END -- Close and deallocate the cursor CLOSE tables DEALLOCATE tables -- Declare cursor for list of indexes to be defragged DECLARE indexes CURSOR FOR SELECT ObjectName, ObjectId, IndexId, LogicalFrag FROM #fraglist WHERE LogicalFrag >= @.maxfrag AND INDEXPROPERTY (ObjectId, IndexName, 'IndexDepth') > 0 -- Open the cursor OPEN indexes -- loop through the indexes FETCH NEXT FROM indexes INTO @.tablename, @.objectid, @.indexid, @.frag WHILE @.@.FETCH_STATUS = 0 BEGIN PRINT 'Executing DBCC INDEXDEFRAG (0, ' + RTRIM(@.tablename) + ', ' + RTRIM(@.indexid) + ') - fragmentation currently ' + RTRIM(CONVERT(varchar(15),@.frag)) + '%' SELECT @.execstr = 'DBCC INDEXDEFRAG (0, ' + RTRIM(@.objectid) + ', ' + RTRIM(@.indexid) + ')' EXEC (@.execstr) FETCH NEXT FROM indexes INTO @.tablename, @.objectid, @.indexid, @.frag END -- Close and deallocate the cursor CLOSE indexes DEALLOCATE indexes -- Delete the temporary table DROP TABLE #fraglist GO |||Here is one way with using sp_MSforeachdb and DBCC DBREINDEXALL
Both of these things are undocumented and you might not want to run it on a production server
EXEC sp_MSForEachDB 'IF ''?'' NOT IN (''master'', ''model'', ''msdb'', ''tempdb'')
DBCC DBREINDEXALL (''?'') '
-- nobody can be connected to the db for the above statement to work
also you could do something like this
DECLARE @.SQL NVarchar(4000)
SET @.SQL = ''
SELECT @.SQL = @.SQL + 'EXEC ' + NAME + '..sp_MSforeachtable @.command1=''DBCC DBREINDEX (''''*'''')'', @.replacechar=''*''' + Char(13)
FROM MASTER..Sysdatabases
where dbid > 6
PRINT @.SQL
EXEC (@.SQL)
However if you exceed 4000 characters this will fail
Denis the SQL Menace
http://sqlservercode.blogspot.com/
|||For EXECUTE you can use varchar(8000), or in 2005 you can use varchar(max).
|||Please don't use undocumented system stored procedures. They are not guaranteed to remain in every version of SQL Server. In fact we removed several system stored procedures or modified their behavior in SQL Server 2005.|||Set options or use statement executed within dynamic SQL is only valid within that scope. So in your case, you have to perform the USE and the DBCC statement within the same dynamic SQL execution like:
exec('use ' + @.somedb + '; dbcc index...')
Sunday, February 12, 2012
Command delivery status in MSrepl_commands
answers on couple of questions.
- How to determine status of command in MSrepl_commands? I mean status is
delivered this command to subscriber or not.
- How long delivered transactions reside in MSrepl_commands?
Have a look at the view msdistribution_status, which should be what you are
looking for. The code of the view is not encrypted and you could use it to
not do the group by and create your own version. If you want the actual
commands, then have a look at sp_browsereplcmds.
How long do commands reside in this table? If they have been read by all the
distribution agents involved and you don't have anonymous subscribers, the
commands will be removed by the cleanup agent. If a distribution agent isn't
synchronized or you have anonymous subscribers, they'll stay there until the
retention period is reached (72 hourd by default), and are then removed by
the cleanup agent.
Rgds,
Paul Ibison SQL Server MVP, www.replicationanswers.com
(recommended sql server 2000 replication book:
http://www.nwsu.com/0974973602p.html)
Friday, February 10, 2012
Come on MSFT guys - I can't be the only one
Problems with Document Map creating a side scroll to get to Up/Down Scroll for the Map itself. There are two scroll bars once you side scroll over in the map window. Anyone seen this or know how to fix?
Anyone know how to change any of the properties on the Document Map or map window?
Please Help!! This is soooooo frustrating
i can′t image that, could you send a screenshot or something like that ?HTH, Jens Suessmeyer.
http://www.sqlserver2005.de|||
Can't exactly figure out how to do that