Monday, April 28, 2014

Oracle Month function


SELECT LAST_NAME, FIRST_NAME, ORIG_HIRE_DT, BIRTHDATE, extract(month from BIRTHDATE)
from ps_pwc_employees
where pwc_mktcls_descr = 'Philadelphia Metro'
and  extract(month from BIRTHDATE) =5

Thursday, April 17, 2014

Schemabinding


CREATE VIEW [dbo].[Transform_Rolling_12_Months]
with schemabinding
AS
SELECT A.Month_ID, B.Month_ID AS Rolling12Month
FROM dbo.Dim_Month AS A LEFT OUTER JOIN dbo.Dim_Month AS B ON B.Month_ID BETWEEN A.Month_ID - 11 AND A.Month_ID
WHERE (A.Month_ID > 11)

GO

Tuesday, April 8, 2014

MS-Access: Suppress System Messages

MS ACCESS: SUPPRESS SYSTEM MESSAGES (QUERY CONFIRMATIONS) IN ACCESS 2003/XP/2000/97

Question: In Microsoft Access 2003/XP/2000/97, how can I suppress the system messages when I run queries? For example, when a delete query is run, Access will ask you to confirm the number of deletions. How can I suppress these kinds of messages?
Answer: To suppress system messages in Access, you will need to use the "Docmd.SetWarnings" command. You could use the following code.
DoCmd.SetWarnings False

{...run queries...}
   
DoCmd.SetWarnings True
For example, you could create a button on your form and place the following code on the Click event:
Private Sub Command1_Click()

   'Turn system messages off
   DoCmd.SetWarnings False
   
   DoCmd.OpenQuery "Delete all entries"
   DoCmd.OpenQuery "Populate with new entries"
   
   'Turn system messages back on
   DoCmd.SetWarnings True
   
End Sub
In this example, we have a button called Command1. When this button is clicked, the system messages will be turned off. Then two queries are run - one called "Delete all entries" and second query called "Populate with new entries".
After the two queries are run, the system messages are turned back on.
The purpose of turning off the system messages is to hide the following kinds of messages from the users:
Microsoft Access

Wednesday, April 2, 2014

Determine Currently Running SQL Server Processes

1.1.1       Determine Currently Running SQL Server Processes

SELECT p.spid
,   right(convert(varchar,            dateadd(ms, datediff(ms, P.last_batch, getdate()), '1900-01-01'),            121), 12) as 'batch_duration'
,   P.program_name
,   P.hostname
,   P.loginame
from master.dbo.sysprocesses P
where P.spid > 50
and      P.status not in ('background', 'sleeping')
and      P.cmd not in ('AWAITING COMMAND'
                    ,'MIRROR HANDLER'
                    ,'LAZY WRITER'
                    ,'CHECKPOINT SLEEP'
                    ,'RA MANAGER')
order by batch_duration
desc


If you need to see the SQL running for a given spid from the results, use something like this:

Declare @spid int,   @stmt_start int,   @stmt_end int,   @sql_handle binary(20) set @spid = XXX -- Fill this in select  top 1    @sql_handle = sql_handle,   @stmt_start = case stmt_start when 0 then 0 else stmt_start / 2 end,   @stmt_end = case stmt_end when -1 then -1 else stmt_end / 2 endfrom    master.dbo.sysprocesseswhere   spid = @spidorder by ecid SELECT    SUBSTRING(    text,                  COALESCE(NULLIF(@stmt_start, 0), 1),                  CASE @stmt_end                         WHEN -1                                THEN DATALENGTH(text)                         ELSE                                (@stmt_end - @stmt_start)                         END           )FROM ::fn_get_sql(@sql_handle)