|
Use Raiserror [^] with a security-level lower than 20.
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|
That's probably not worth the effort; just catch the Exception in the calling code.
And don't prefix your procedures with sp_ -- that's for System Procedures.
|
|
|
|
|
Good day,
I want to retrieve records from sql server 2005 that belongs to today,
my datatype in sqlserver is : datetime and store date like : 9/5/2012 12:55:26 PM
So i want to know how can I write a query to retrieve only today's records.
Thanks in advance
|
|
|
|
|
0) I hope you're using a DATETIME field and not storing dates as strings. Otherwise you're hosed.
1) If this is a recent version of SQL Server you can try WHERE datefield >= CAST(GETDATE() AS DATE)
modified 5-Sep-12 23:28pm.
|
|
|
|
|
i have found it
WHERE LASTUPDATEON >= DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0) AND LASTUPDATEON < DATEADD(day, DATEDIFF(day, 0, GETDATE()), 1)
|
|
|
|
|
WTE!
Which database system?
Why would you have to eliminate records that will be updated tomorrow?
"Simplify. Simpify." -- Thoreau
|
|
|
|
|
|
That is very very bad. Highly inefficient. You convert every (millions?) SaveDate value and then do a string compare -- which is much less efficient than the DATETIME compare would be.
<anecdote>
I once fixed a program that was written that way -- before I fixed it it took forty minutes to run, afterward it took only ten minutes.
</anecdote>
DON'T EVER DO THAT!!
|
|
|
|
|
Hello,
I am very green to sql, and normally program in c sharp. I have 2 GUI generated sql queries that I'd like to combine, then exclude the results (users) that are found in the second query, from the first query and finally return the new result set that has the second query data removed from the first data query.
Here are the queries:
Thanks for reading.
SELECT AED.ObjectName,
AED.ObjectCanonical,
AEM.EventMessage,
AED.OrganizationalUnit AS DirectoryOrganizationalUnit,
AET.UserName,
AET.SeverityID,
AET.TimeDetected,
AET.ValueOld,
AET.ValueNew,
ECS.SubsystemID,
ISNULL(ECS.CustomEventClassName, ECS.EventClassName) AS EventClassName,
ISNULL(TSR.ServerName, TWP.MachineName) AS ServerName,
EAN.ActionName,
ISNULL(TDN.DomainName, TWP.WorkgroupName) AS DomainName,
AET.ResultID,
AET.EventID,
ECS.ValueTypeID,
AET.MissingOld,
AET.MissingNew,
AET.TimeZoneOffset
FROM Audit.Event AS AET
INNER JOIN [Event].Class AS ECS
ON AET.EventClassID = ECS.EventClassID
INNER JOIN Audit.EventMessage AS AEM
ON AET.EventID = AEM.EventID
INNER JOIN [Event].[Action] AS EAN
ON AET.ActionID = EAN.ActionID
LEFT OUTER JOIN Topology.[Server] AS TSR
ON AET.AgentID = TSR.ServerID
LEFT OUTER JOIN Topology.Domain AS TDN
ON TSR.DomainID = TDN.DomainID
LEFT OUTER JOIN Topology.Workgroup AS TWP
ON AET.AgentID = TWP.MachineID
LEFT OUTER JOIN Audit.EventDirectory AS AED
ON AET.EventID = AED.EventID
WHERE AET.TimeDetected >= '2012-06-07T04:00:00'
AND EXISTS(SELECT AET.EventID
FROM [Event].Class AS _EC
WHERE EXISTS(SELECT *
FROM Query.EventClass AS _ECQ
WHERE _ECQ.QueryID = '2E84F194-03D0-4218-A346-76C50B98E36A'
AND _ECQ.ProcessID = 0
AND StorageClassID = 0
AND _ECQ.EventClassID = _EC.EventClassID)
AND _EC.EventClassID = AET.EventClassID)
AND NOT EXISTS(SELECT *
FROM (SELECT _QA.SID AS UserSID
FROM Query.[Account] AS _QA
WHERE _QA.QueryID = '2E84F194-03D0-4218-A346-76C50B98E36A'
AND _QA.ProcessID = 0) AS _SA
WHERE _SA.UserSID = AET.UserSID)
ORDER BY TimeDetected DESC,
UserName DESC
SELECT AED.ObjectName,
AED.ObjectCanonical,
AED.OrganizationalUnit AS DirectoryOrganizationalUnit,
AEM.EventMessage,
AET.TimeDetected,
ECS.SubsystemID,
AET.UserName,
ISNULL(ECS.CustomEventClassName, ECS.EventClassName) AS EventClassName,
ISNULL(TSR.ServerName, TWP.MachineName) AS ServerName,
EAN.ActionName,
ISNULL(TDN.DomainName, TWP.WorkgroupName) AS DomainName,
AET.ResultID,
AET.EventID,
ECS.ValueTypeID,
AET.MissingOld,
AET.MissingNew,
AET.TimeZoneOffset
FROM Audit.Event AS AET
INNER JOIN [Event].Class AS ECS
ON AET.EventClassID = ECS.EventClassID
INNER JOIN Audit.EventMessage AS AEM
ON AET.EventID = AEM.EventID
INNER JOIN [Event].[Action] AS EAN
ON AET.ActionID = EAN.ActionID
LEFT OUTER JOIN Topology.[Server] AS TSR
ON AET.AgentID = TSR.ServerID
LEFT OUTER JOIN Topology.Domain AS TDN
ON TSR.DomainID = TDN.DomainID
LEFT OUTER JOIN Topology.Workgroup AS TWP
ON AET.AgentID = TWP.MachineID
LEFT OUTER JOIN Audit.EventDirectory AS AED
ON AET.EventID = AED.EventID
WHERE AET.TimeDetected >= '2012-06-07T04:00:00'
AND EXISTS(SELECT AET.EventID
FROM [Event].Class AS _EC
WHERE EXISTS(SELECT *
FROM Query.EventClass AS _ECQ
WHERE _ECQ.QueryID = 'C37D85D6-B725-4E98-A338-B8917CA5784F'
AND _ECQ.ProcessID = 0
AND StorageClassID = 0
AND _ECQ.EventClassID = _EC.EventClassID)
AND _EC.EventClassID = AET.EventClassID)
ORDER BY TimeDetected DESC,
UserName DESC
|
|
|
|
|
Read up on Union or Union All
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
You can use "Except Distinct" !
|
|
|
|
|
INTERSECT might also be useful for this scenario.
|
|
|
|
|
Hello,
I have tried inserting those commands into various locations in the query, without success. Could someone give me an example please?
|
|
|
|
|
use EXCEPT between two queries
|
|
|
|
|
Someone helped me come up with this code today, it appears to be producing the proper results, what does every one think?
It uses And UserName Not In, and brings in the second search to compare.
-- Mailbox size query users who are set to unlimited, distinct username field with disabled mailboxes removed from search
SELECT AED.ObjectName,
AED.ObjectCanonical,
AEM.EventMessage,
AED.OrganizationalUnit AS DirectoryOrganizationalUnit,
AET.UserName,
AET.SeverityID,
AET.TimeDetected,
AET.ValueOld,
AET.ValueNew,
ECS.SubsystemID,
ISNULL(ECS.CustomEventClassName, ECS.EventClassName) AS EventClassName,
ISNULL(TSR.ServerName, TWP.MachineName) AS ServerName,
EAN.ActionName,
ISNULL(TDN.DomainName, TWP.WorkgroupName) AS DomainName,
AET.ResultID,
AET.EventID,
ECS.ValueTypeID,
AET.MissingOld,
AET.MissingNew,
AET.TimeZoneOffset
FROM Audit.Event AS AET
INNER JOIN [Event].Class AS ECS
ON AET.EventClassID = ECS.EventClassID
INNER JOIN Audit.EventMessage AS AEM
ON AET.EventID = AEM.EventID
INNER JOIN [Event].[Action] AS EAN
ON AET.ActionID = EAN.ActionID
LEFT OUTER JOIN Topology.[Server] AS TSR
ON AET.AgentID = TSR.ServerID
LEFT OUTER JOIN Topology.Domain AS TDN
ON TSR.DomainID = TDN.DomainID
LEFT OUTER JOIN Topology.Workgroup AS TWP
ON AET.AgentID = TWP.MachineID
LEFT OUTER JOIN Audit.EventDirectory AS AED
ON AET.EventID = AED.EventID
WHERE AET.TimeDetected >= '2012-06-08T04:00:00'
AND EXISTS(SELECT AET.EventID
FROM [Event].Class AS _EC
WHERE EXISTS(SELECT *
FROM Query.EventClass AS _ECQ
WHERE _ECQ.QueryID = '2E84F194-03D0-4218-A346-76C50B98E36A'
AND _ECQ.ProcessID = 0
AND StorageClassID = 0
AND _ECQ.EventClassID = _EC.EventClassID)
AND _EC.EventClassID = AET.EventClassID)
AND NOT EXISTS(SELECT *
FROM (SELECT _QA.SID AS UserSID
FROM Query.[Account] AS _QA
WHERE _QA.QueryID = '2E84F194-03D0-4218-A346-76C50B98E36A'
AND _QA.ProcessID = 0) AS _SA
WHERE _SA.UserSID = AET.UserSID)
AND ValueNew = '<Not-Set>'
AND UserName NOT IN (
SELECT DISTINCT --AED.ObjectName,
--AED.ObjectCanonical,
-- AED.OrganizationalUnit AS DirectoryOrganizationalUnit,
-- AEM.EventMessage,
-- AET.TimeDetected,
-- ECS.SubsystemID,
AET.UserName--,
-- ISNULL(ECS.CustomEventClassName, ECS.EventClassName) AS EventClassName,
-- ISNULL(TSR.ServerName, TWP.MachineName) AS ServerName,
-- EAN.ActionName,
-- ISNULL(TDN.DomainName, TWP.WorkgroupName) AS DomainName,
-- AET.ResultID,
-- AET.EventID,
-- ECS.ValueTypeID,
-- AET.MissingOld,
-- AET.MissingNew,
-- AET.TimeZoneOffset
FROM Audit.Event AS AET
INNER JOIN [Event].Class AS ECS
ON AET.EventClassID = ECS.EventClassID
INNER JOIN Audit.EventMessage AS AEM
ON AET.EventID = AEM.EventID
INNER JOIN [Event].[Action] AS EAN
ON AET.ActionID = EAN.ActionID
LEFT OUTER JOIN Topology.[Server] AS TSR
ON AET.AgentID = TSR.ServerID
LEFT OUTER JOIN Topology.Domain AS TDN
ON TSR.DomainID = TDN.DomainID
LEFT OUTER JOIN Topology.Workgroup AS TWP
ON AET.AgentID = TWP.MachineID
LEFT OUTER JOIN Audit.EventDirectory AS AED
ON AET.EventID = AED.EventID
WHERE AET.TimeDetected >= '2012-06-07T04:00:00'
AND EXISTS(SELECT AET.EventID
FROM [Event].Class AS _EC
WHERE EXISTS(SELECT *
FROM Query.EventClass AS _ECQ
WHERE _ECQ.QueryID = 'C37D85D6-B725-4E98-A338-B8917CA5784F'
AND _ECQ.ProcessID = 0
AND StorageClassID = 0
AND _ECQ.EventClassID = _EC.EventClassID)
AND _EC.EventClassID = AET.EventClassID)
)
ORDER BY ObjectName
--ORDER BY TimeDetected DESC,
-- UserName DESC
|
|
|
|
|
Suppose I have a table in a database (TblExample) contained three columns (Name, Age, City). Now after sometimes someone added one extra column (country) or deleted the column(City) or rename the column (Name to FirstName). Now, how I will get the added, deleted or renamed column of the Table.
Please reply ASAP
Thanks.
|
|
|
|
|
You will need to build a schema compare utility.
Make a connection to each of the databases you want to compare and you will have to walk through each one of the tables and compare the column definitions.
Look at sys.columns
http://msdn.microsoft.com/en-us/library/ms176106.aspx[^]
|
|
|
|
|
Then you need to add a ton of artificial intelligence and still guess wrong.
|
|
|
|
|
I disagree with the comment about "artificial intelligence" and "guessing".
The question was How I will get the added, deleted or renamed column of the Table.
My response gave a viable option for him to determine the difference between tables in two databases. I did not try to read into the 'why' or 'what for'.
There is no reason to be sarcastic when replying. Just stay professional.
Now let me get my flak jacket and fireman coat before the responses come flying in.
|
|
|
|
|
And my point is that seeing the differences in the schema won't show you how you got there, what changes were made.
For example, if A,B,C changed to A,D,C -- Was B renamed to D? Was B deleted and D added? There is no way to know with the information given. You may also look at the datatype, but you may also need to investigate the contents of the columns to see whether or not D contains the same contents as B did.
In such a scenario (and based on some data I'm working on now) maybe the original was TRANSACTION_D_T VARCHAR(20) (in yyyy-MM-dd HH:MM:ss format) and it was changed to TRANSACTION_DATE DATETIME -- how would you determine what change was made simply by looking at the schema?
|
|
|
|
|
I agree that seeing a difference in a schema does not tell you how you got there, but the simple matter of detecting a change will initiate an investigation.
No change = No problem (ideally)
Change = Investigation
When there is team development on an application there must be communication among the team to reflect any changes. Using a source control system like Subversion is a good way to see changes made and designating a single person as the responsible party for making any changes to the schema is also a good idea.
|
|
|
|
|
David Mujica wrote: My response gave a viable option for him to determine the difference between tables in two databases. I did not try to read into the 'why' or 'what for'.
Good point; yet there are certain dangers to the approach, which should also be mentioned if one is to make an informed decision.
It's good to see a broad variety of answers, each with their own motivation. You're right that my comment was superfluous after the warning of PIEBALD, and reading back it indeed looks a tad patronizing.
David Mujica wrote: There is no reason to be sarcastic when replying. Just stay professional.
..will try, a little bit harder. Then again, sarcasm is an efficient way to get a point across and to create a few seconds of shock in which one contemplates the previously ignored point.
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|
I always appreciate the help I get from the folks here.
Sometimes I have to resist jumping on the dog-pile. There are many posts that I just don't respond to because they are just not worth my time.
Thanks again for your (and others) contribution to the community; I've learned a lot here.
David
|
|
|
|
|
You attend project meetings where the DBA tells you.
|
|
|
|
|
I agree with PIEBALD; you do not know why the columns are altered, so best idea is to ignore the changes, and declare "incompatibility with the current version" if they're not as you expected them to be.
If a column is added, it's not always desired to have it visible; if it is visible, it will have consequences for the layout, which means some changes to reports.
If a column is deleted, it's not always desired to ignore it; it could have been renamed during an update, with the intention of renaming it back later - your behaviour would hide a very important bug.
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|