Tuesday, January 27, 2009

Some SQL Snippets

Posted by David Jennaway


Over time (don't ask how long – suffice to say I first used Microsoft SQL Server on OS/2) you pick up a fair amount of useful SQL Server knowledge. This post is intended to be a random collection of snippets that I use and remember, and I expect to add to the post periodically as I encounter further uses for SQL knowledge. So, in no particular order...

Finding SQL objects that contain a particular string
The definition of SQL objects can be accessed via the sys.syscomments view in the SQL database, and can be queried. The following example returns the name of objects that contain 'Test' somewhere within the definition. The object_name function is a quick way to get the name of an object from its id – the other way is to join to the sys.objects view.

select distinct object_name(id) from sys.syscomments where text like '%Test%'

Note that this only works if the SQL object definition was not encrypted with the WITH ENCRYPTION option

Granting Permissions to a set of objects
I've yet to find a good user-interface in SQL for setting permissions on a set of objects quickly, so I tend to use SQL commands. The following procedure shows how to use a cursor to iterate through a set of objects and execute a dynamically-built GRANT statement on them

declare cur cursor fast_forward for


 


select name from sys.objects


where type = 'V' and name like 'vw_%' -- Get all views, beginning vw_


 


declare @obj sysname, @sql nvarchar(2000)


open cur


fetch next from cur into @obj


while @@fetch_status = 0


begin


set @sql = 'GRANT SELECT ON ' + @obj + ' TO public'


-- grant select permission to public


exec (@sql)


fetch next from cur into @obj


end


close cur


deallocate cur 




Outputting stored procedure information to a table

There are cases when you might want to use the results of a stored procedure in a table structure for future processing. There's not an EXECUTE INTO statement but you can use INSERT ... EXECUTE. You can also use this with dynamically constructed SQL, using EXECUTE (@sql). The following example uses both EXECUTE syntaxes, and shows how to iterate though the names of 'tables' from a linked server – this is used to query Excel spreadsheets where there is a dynamic range of identically structured worksheets





create table #excelsheets -- Store names of spreadsheets in Excel


( TABLE_CAT sysname null


,TABLE_SCHEM sysname null


,TABLE_NAME sysname not null


,TABLE_TYPE sysname null


,REMARKS nvarchar(255) null )


 


insert #excelsheets execute sp_tables_ex 'EXCELDYNAMIC'


-- EXCELDYNAMIC is a linked server


 


create table #tmp


-- Temporary storage of data, so results can be output as one result set


( TABLE_NAME sysname


,[Month] int


,[Target] decimal(10,2) )


 


declare cur cursor fast_forward


for select TABLE_NAME from #excelsheets


declare @tbl sysname, @sql nvarchar(4000)


open cur


fetch next from cur into @tbl


while @@fetch_status = 0


begin


-- Build dynamic SQL statement. It would be nice to pass the statement as a parameter to OPENQUERY, but that's not permitted


set @sql = 'Select ''' + @tbl + ''' as TABLE_NAME, [Month], [Target] FROM EXCELDYNAMIC...[' + @tbl + ']'


insert #tmp exec (@sql)


fetch next from cur into @tbl


end


-- Cleanup and output results


close cur


deallocate cur


select * from #tmp


drop table #tmp


drop table #excelsheets


 




Posted by David Jennaway

No comments: