Monday, March 12, 2012

Marking all stored procedures in a database for recompilation

I've seen a few of these scripts and they are all more complicated than they need to be. Here's a basic one (SQL 2008).


DECLARE @sql NVARCHAR(MAX) SET @sql = ''
SELECT @sql = @sql + 'EXEC sp_recompile ''[' + OBJECT_SCHEMA_NAME(object_id) + '].[' + OBJECT_NAME(object_id) + ']'';' + CHAR(13) + CHAR(10)
FROM sys.procedures

PRINT @sql
EXEC(@sql)

Wednesday, September 15, 2010

An INSERT EXEC statement cannot be nested

I ran into the following problem: if a procedure is using INSERT EXEC, and you try to INSERT EXEC that procedure, you get the following error message:
An INSERT EXEC statement cannot be nested.

Example:

USE MyDB
GO

CREATE PROC dbo.Proc1
AS
SELECT 1
GO

CREATE PROC dbo.Proc2
AS
SET NOCOUNT ON
CREATE TABLE #t (value INT)
INSERT INTO #t
EXEC Proc1
SELECT * FROM #t
GO

CREATE TABLE #results (value INT)

INSERT INTO #results
EXEC dbo.Proc2

An INSERT EXEC statement cannot be nested.

I found the following simple workaround:

INSERT INTO #results
SELECT * FROM OPENROWSET('SQLNCLI', 'server=(local);trusted_connection=yes', 'EXEC MyDB.dbo.Proc2')

Tuesday, February 10, 2009

Exporting SQL Server Procedures and Functions to Files

Sometimes it is necessary to export all or some of the stored procedures and functions in a database to files (one file per procedure). This is useful for example if you want to place all your database scripts under source control.

The bcp utility shipped with SQL Server can help. The following query generates one bcp command line per procedure. All commands can then be copied to a batch file and executed from the command line.

In the query below replace and with the appropriate values. Also note that the -T parameter stands for Trusted Authentication, but it is possible to specify a username and password using the -U and -P parameters of the bcp utility (more details and other parameters here).


select 'bcp "SELECT routine_definition from .information_schema.routines where routine_schema = ''' + routine_schema + ''' and routine_name='''
+ routine_name + '''" QueryOut "c:\temp\procs\'
+ routine_schema + '.' + routine_name + '.sql" -c -S -T'
from information_schema.routines

Labels:

Friday, January 18, 2008

Generate INSERT statements from SQL Server table data

It is often necessary to copy data between two tables (for example between staging and production databases). SQL Server makes this task rather complicated (you have to resort to export/import). In MySQL it is possible to generate INSERT statements for all the records in a table, and some SQL Server client tools (such as the somewhat immature but excellent Toad) also do this.

The really low cost solution though is to use this stored procedure:
http://vyaskn.tripod.com/code.htm#inserts

Wednesday, July 25, 2007

SQL Server: Executing Jobs Synchronously

SQL Server jobs can be started from T-SQL using msdb.dbo.sp_start_job:

EXEC msdb.dbo.sp_start_job @job_name = 'My job name'

This call however will return immediately (i.e. the job executes asynchronously). Sometimes it is desirable for the job to be executed synchronously.  The following procedure accomplishes this.

Usage

Parameters:

  • @jobID UNIQUEIDENTIFIER (optional, only required if @jobName is not provided)
  • @jobName NVARCHAR(500) (optional, only required if @jobID is not provided)
  • @timeoutSeconds INT (optional, default 60 seconds)

Remarks:

  • The procedure will throw an error if the timeout period is reached before the job completes
  • The procedure will throw an error if the job fails

Examples:

EXEC common.dbo.execute_job_synchronously '2696980E-1E38-43C4-8350-29DBFC56974D', null, 30

EXEC common.dbo.execute_job_synchronously null, ‘My cool job’

EXEC common.dbo.execute_job_synchronously null, ‘My cool job’, 3600

Thursday, July 05, 2007

PONG

Tuesday, June 26, 2007

Surface Computing

Cool Phone Hacks

I came across these neat services (free during beta testing) on http://4hourworkweek.com.
 
http://jott.com  uses voice recognition to transcribe your spoken message into an SMS or email. The way they put it, "send SMS with your voice." Great for emailing yourself reminders, too.
 
http://pinger.com  lets you send voicemail to people without calling them. Perfect for late night messages or when you don't really want to talk to the recipient... (Next courtship ritual: breaking up on pinger.) Also great for messaging multiple people at once. Think of it as voice email.
 
http://grandcentral.com is your personal assistant that screens and routes calls and much, much more. My favorite feature: designating certain callers as SPAM. Future calls from those numbers automatically get a "number not in service" message when they call you. Genius.
 
 

The Visit

I thought this was really cool--a sign of things to come in our society. Crowd-financed movies... (Maybe there's hope for humanity after all.) I look forward to the day when blockbusters are financed with lunch money by kids who vote on what happens to Spidey in the next installment. (There's no hope for humanity.)