SQL Server Python Tutorial


See these additional resources for more R samples:

How to enable sp_execute_external_script to run R script in Sql server?

================ Error ===================

Msg 39023, Level 16, State 1, Procedure sp_execute_external_script, Line 1 [Batch Start Line 9]

‘sp_execute_external_script’ is disabled on this instance of SQL Server. Use sp_configure ‘external scripts enabled’ to enable it.

Msg 11536, Level 16, State 1, Line 10

EXECUTE statement failed because its WITH RESULT SETS clause specified 1 result set(s), but the statement only sent 0 result set(s) at run time.

========================= fix ===========================

when enabled, you will see the config_value changed from 0 to 1

Note: SQL Server Service has to be restart for this to take into effect!


SQL Server how to run as another user?

EXECUTE AS LOGIN = ‘login1’;
–Verify the execution context is now login1.
–Login1 sets the execution context to login2.
EXECUTE AS USER = ‘user2’;
–Display current execution context.
— The execution context stack now has three principals: the originating caller, login1 and login2.
–The following REVERT statements will reset the execution context to the previous context.

monitor sql server job status

SELECT name AS [Job Name]
,CONVERT(VARCHAR,DATEADD(S,(run_time/10000)*60*60 /* hours */
+((run_time – (run_time/10000) * 10000)/100) * 60 /* mins */
+ (run_time – (run_time/100) * 100)  /* secs */
,CONVERT(DATETIME,RTRIM(run_date),113)),100) AS [Time Run]
,CASE WHEN enabled=1 THEN ‘Enabled’
ELSE ‘Disabled’
END [Job Status]
,CASE WHEN SJH.run_status=0 THEN ‘Failed’
WHEN SJH.run_status=1 THEN ‘Succeeded’
WHEN SJH.run_status=2 THEN ‘Retry’
WHEN SJH.run_status=3 THEN ‘Cancelled’
ELSE ‘Unknown’
END [Job Outcome]
FROM   sysjobhistory SJH
JOIN   sysjobs SJ
ON     SJH.job_id=sj.job_id
WHERE  step_id=0
(run_time/10000)*60*60 /* hours */
+((run_time – (run_time/10000) * 10000)/100) * 60 /* mins */
+ (run_time – (run_time/100) * 100)  /* secs */,
CONVERT(DATETIME,RTRIM(run_date),113)) >= DATEADD(d,-1,GetDate())
ORDER BY name,run_date,run_time