Archive for SQL Server

21 Nov 2008

Generating JSON from TSQL Query

No Comments SQL Server

I was looking for TSQL implementation which can build JSON payload directly from database. After couple of failed search, I decided to create my own stored procedure which can render JSON based for any query passed as parameter. After few keystrokes in SQL Server Management Studio, I have this semi-finish stored procedure which I thought you will find useful.

CREATE PROCEDURE [dbo].[GetJSON] (
@ParameterSQL AS VARCHAR(MAX)
)
AS
BEGIN
 
DECLARE @SQL NVARCHAR(MAX)
DECLARE @XMLString VARCHAR(MAX)
DECLARE @XML XML
DECLARE @Paramlist NVARCHAR(1000)
SET @Paramlist = N'@XML XML OUTPUT'
SET @SQL = 'WITH PrepareTable (XMLString) '
SET @SQL = @SQL + 'AS ( '
SET @SQL = @SQL + @ParameterSQL+ ' FOR XML RAW, TYPE, ELEMENTS '
SET @SQL = @SQL + ') '
SET @SQL = @SQL + 'SELECT @XML = XMLString FROM PrepareTable '
EXEC sp_executesql @SQL, @Paramlist, @XML=@XML OUTPUT
SET @XMLString = CAST(@XML AS VARCHAR(MAX))
 
DECLARE @JSON VARCHAR(MAX)
DECLARE @Row VARCHAR(MAX)
DECLARE @RowStart INT
DECLARE @RowEnd INT
DECLARE @FieldStart INT
DECLARE @FieldEnd INT
DECLARE @KEY VARCHAR(MAX)
DECLARE @Value VARCHAR(MAX)
 
DECLARE @StartRoot VARCHAR(100); SET @StartRoot = '<row>'
DECLARE @EndRoot VARCHAR(100); SET @EndRoot = '</row>'
DECLARE @StartField VARCHAR(100); SET @StartField = '<'
DECLARE @EndField VARCHAR(100); SET @EndField = '>'
 
SET @RowStart = CharIndex(@StartRoot, @XMLString, 0)
SET @JSON = ''
WHILE @RowStart > 0
BEGIN
	SET @RowStart = @RowStart+Len(@StartRoot)
	SET @RowEnd = CharIndex(@EndRoot, @XMLString, @RowStart)
	SET @Row = SubString(@XMLString, @RowStart, @RowEnd-@RowStart)
	SET @JSON = @JSON+'{'
 
	-- for each row
	SET @FieldStart = CharIndex(@StartField, @Row, 0)
	WHILE @FieldStart > 0
	BEGIN
		-- parse node key
		SET @FieldStart = @FieldStart+Len(@StartField)
		SET @FieldEnd = CharIndex(@EndField, @Row, @FieldStart)
		SET @KEY = SubString(@Row, @FieldStart, @FieldEnd-@FieldStart)
		SET @JSON = @JSON+'"'+@KEY+'":'
 
		-- parse node value
		SET @FieldStart = @FieldEnd+1
		SET @FieldEnd = CharIndex('</', @Row, @FieldStart)
		SET @Value = SubString(@Row, @FieldStart, @FieldEnd-@FieldStart)
		SET @JSON = @JSON+'"'+@Value+'",'
 
		SET @FieldStart = @FieldStart+Len(@StartField)
		SET @FieldEnd = CharIndex(@EndField, @Row, @FieldStart)
		SET @FieldStart = CharIndex(@StartField, @Row, @FieldEnd)
	END	
	IF LEN(@JSON)>0 SET @JSON = SubString(@JSON, 0, LEN(@JSON))
	SET @JSON = @JSON+'},'
	--/ for each row
 
	SET @RowStart = CharIndex(@StartRoot, @XMLString, @RowEnd)
END
IF LEN(@JSON)>0 SET @JSON = SubString(@JSON, 0, LEN(@JSON))
SET @JSON = '[' + @JSON + ']'
SELECT @JSON
 
END

To use this stored procedure, just pass TSQL query as parameter. Something like this:

EXEC GetJSON 'SELECT * FROM dbo.Employee_TBL'

And this stored procedure will return JSON payload:

[{"UserId":"7C92EB27-DD81-498E-82CE-18192C940328","FirstName":"Bill","LastName":"Gates","Age":"45","LastLogin":"2008-11-02T00:00:00"},{"UserId":"58E75687-8D0F-423A-8A76-4B1D750F62FD","FirstName":"Steve","LastName":"Job","Age":"52","LastLogin":"2008-10-17T00:00:00"}]
15 Nov 2008

Getting list of modified objects in SQL Server

No Comments SQL Server, Uncategorized

Developers always want to have a list of modified tables or stored procedures while check-in the code or wrapping up the development at the end of the day. some usually keep this list in text file and they update this file every time they modify any table or stored procedure. Here are some handy SQL snippets which can provide you list of the tables, or stored procedure modified in specific duration.

-- tables modified today
SELECT [name],create_date,modify_date
FROM sys.objects
WHERE modify_date>DATEADD(day,-1,GETDATE())
AND type='U'
-- stored procedures modified today
SELECT [name],create_date,modify_date
FROM sys.objects
WHERE modify_date>DATEADD(day,-1,GETDATE())
AND type='P'

You can further modify above queries to get list with objects modified in last 7 days.

-- tables modified in last 7 days
SELECT [name],create_date,modify_date
FROM sys.objects
WHERE modify_date>DATEADD(day,-7,GETDATE())
AND type='U'
05 Apr 2008

Released – TinySQL v2.0

No Comments Code Generation, Programming, SQL Server

I just released TinySQL code generator v2.0 with few bug fixes and a new feature of using separator tag for loop statement. You can now use {sap}{/sap} tags to place your separators inside loop.

Here is the example of using {sap}{/sap} tags in TinySQL template:

** Generate DATA Provider: UPDATE/v2.0
CREATE PROCEDURE [dbo].[usp_Update$table]{loop}
	@$field = $sp_type{sap},{/sap}{/loop}
AS
BEGIN
UPDATE dbo.$table WITH (ROWLOCK)
SET {loop}$field = @$field{sap},{/sap}
	{/loop}
WHERE --TODO
RETURN -1
END

And generated code from above template will be like:

CREATE PROCEDURE [dbo].[usp_UpdateAction]
	@Id = NUMERIC,
	@Action = VARCHAR(250),
	@ProjectId = NUMERIC,
	@IsNextAction = BIT,
	@IsWaitingFor = BIT,
	@WaitingForNotes = VARCHAR(1000),
	@RemindOn = DATETIME,
	@IsDefer = BIT,
	@DeferDate = DATETIME,
	@IsDone = BIT,
	@Sequence = INT
AS
BEGIN
UPDATE dbo.Action WITH (ROWLOCK)
SET Id = @Id,
	Action = @Action,
	ProjectId = @ProjectId,
	IsNextAction = @IsNextAction,
	IsWaitingFor = @IsWaitingFor,
	WaitingForNotes = @WaitingForNotes,
	RemindOn = @RemindOn,
	IsDefer = @IsDefer,
	DeferDate = @DeferDate,
	IsDone = @IsDone,
	Sequence = @Sequence
WHERE --TODO
RETURN -1
END

Download: TinySQL v2.0

You can also read my previous blog to learn more about TinySQL tool.
TinySQL Code Generator

Hope you will find this tool useful.

25 May 2006

Intellisense for SQL Server

No Comments SQL Server

So finally, SQL Query Analyzer has got feature which I was looking for long time. It’s SQL Prompt from Red Gate Software Ltd.

Intellisense for SQL Server

SQL Prompt provides intellisense support for Microsoft SQL Server editors. It will tell you the exact formatting you should use for your SQL commands, while you are writing them and will therefore help you write fast, perfectly formatted SQL statements. SQL Prompt improves the productivity of all SQL script creation.

You can download free version SQL Prompt from following location:
Downlod SQL Prompt

17 May 2006

CodePlex

No Comments .NET, SQL Server

I just heard about CodePlex from Microsoft. After browsing its some core features, I can say that this is really a cool online development environment. You can use CodePlex to create projects and share with your fellow developers. Or you can also join other projects of your interest that already have started. With that, you can also add some project management tools like forum, issue tracker, source code dissemination, project team communicator, news feed aggregator etc. to any CodePlex project.

CodePlex appears more or less like that old GotDotNet except this has very simple and clean user interface and very improved performance. So I guess this should be replacement of GotDotNet project.