Showing posts with label store. Show all posts
Showing posts with label store. Show all posts

Thursday, February 16, 2012

Cross Reference or Pivot table

Hi All,

The problem is about cross reference.

1. I have a third party cross reference store procedure SimpleXTab


CREATE PROCEDURE [dbo].[SimpleXTab2]
@.XField varChar(50),
@.XTable varChar(100),
@.XWhereString varChar(250),
@.XFunction varChar(10),
@.XFunctionField varChar(50),
@.XRow varchar(300),
@.ResultTable varchar(100)
AS
Declare @.SqlStr nvarchar(4000)
Declare @.tempsql nvarchar(4000)
Declare @.SqlStrCur nvarchar(4000)
Declare @.col nvarchar(100)

set @.SqlStrCur = N'Select [' + @.XField + '] into ##temptbl_Cursor from [' + @.XTable + '] ' + @.XWhereString + ' Group By [' + @.XField + ']'

/* select @.sqlstrcur */
exec sp_executesql @.sqlstrcur

declare xcursor Cursor for Select * from ##temptbl_Cursor

open xcursor


Fetch next from xcursor
into @.Col

While @.@.Fetch_Status = 0
Begin
set @.Sqlstr = @.Sqlstr + ", "
set @.tempsql = isnull(@.sqlstr,'') + isnull(@.XFunction + '( Case When ' + @.XField + " = '"+@.Col +
"' then [" + @.XFunctionField + "] Else 0 End) As [" + @.Col + "]" ,'')
set @.Sqlstr = @.tempsql
Fetch next from xcursor into @.Col

End


/* Select @.Sqlstr as [mk], len(@.sqlstr) as [leng] */

set @.tempsql = 'Select ' + @.XRow + ', ' + @.Sqlstr + 'into '+@.ResultTable+' From ' + @.XTable +
@.XWhereString + ' Group by ' + @.XRow
print @.tempsql
set @.Sqlstr = @.tempsql

Close xcursor
Deallocate xcursor

set @.tempsql = N'Drop Table ##temptbl_Cursor'
exec sp_executesql @.tempsql
print @.tempsql
/* Select @.Sqlstr as [mk], len(@.sqlstr) as [leng] */
print @.sqlstr
exec sp_executesql @.Sqlstr

if @.@.rowcount = 0 select 'No Records found'
GO

2. I've use this store procedure for many cross reference successfully. But this time my cross reference value (resultcode) is a varchar which cannot be convert to int or decimal in sql, Probably, you've noticed that the fourth parameter is a function.
how can i modify SimpleXtab to avoid using math function but still can generate cross reference.

exec simplextab2 'Sequence','##tbltempreport',' ','sum','resultcode','Parameter' ,'dbo.resultcodetable'

Many Thanks!

no replying. I didn't describe clearly?

|||

What did you want it to do with multiple values? If there is only one value per row/column, use MAX or MIN.

Cross Referance dB objects accross store procs

We are in the midst of cleaning up database01. There are objects in database01 that are widely used in store procedures that reside in database02.

Problem is: I need to know which tables and view from database01 are in use by store procedures in database02. This is important because if I accidently decommission tables that are currently in use by production class s-procedures, it will not be pretty. A sample output could look like:

StoreProc UsingObject
sp001 ctblCodeName
sp002 tblUnitSales
so003 tblSalesTrans
.......... .................

I am not a programmer and none of our programmers here claim that there is any solution to this problem without spending thousands on s/w licenses. Any simple solution + code snippet that will help me resolve this problem by myself would be incredibly valuable.

In disstress and frustration . . . .

JJOSHI

Hi JJOSHI,

There is no full-proof method of finding this information, though you could try a couple of things to get some pointers:

1) Check the sys.sql_dependencies catalog view for information (see books online for more information, if this is Sql 2005)

2) Try searching through the syscomments table (which is the table that holds the definition for all code modules in Sql)

Note that this doesn't capture references of things like foreign keys, constraints, etc....

There are some 3rd party components that try to give you this information, but MS doesn't guarantee this information, and supportability would be only through that company.

For searching through the syscomments, you could try something like this (NOT SUPPORTED IN ANY WAY BY ANYONE):

declare @.name varchar(500), @.sql varchar(1000)

select @.name = '%' + '<put object name here>' + '%'

-- Any jobs with it?

select j.name as JobName, s.step_name, s.command, s.database_name, j.enabled

from msdb.dbo.sysjobsteps s

join msdb.dbo.sysjobs j

on j.job_id = s.job_id

where command like(@.name)

-- Any db's reference it?

select @.sql = 'use [?] select object_name(id) as objectname, db_name() as dbname, substring(text, patindex(''' + @.name + ''', text) - 20,150) from syscomments where text like(''' + @.name + ''')'

exec master.dbo.sp_MSforeachdb @.sql

|||

To add to what has already been said, in 2005, you can use the object_definition function. I had written a blog about this a while back (http://drsql.spaces.live.com/blog/cns!80677FB08B3162E4!1139.entry) to cover something like this. Since object_definition includes the whole object text in a single part (syscomments breaks it up into chunks in 2000) you can use a query like:

DECLARE @.value nvarchar(128)
SET @.value = '<databaseName>.'

SELECT cast(schema_name(schema_id) + '.' + name AS varchar(60)) AS name,
cast(type_desc AS varchar(20)) AS type , create_date,
modify_date,
char(13) + char(10)
+ '--select object_definition(' + cast(object_id as varchar(10)) + ') as [' + name + ']'
FROM sys.objects
WHERE charindex(@.value,replace(replace(object_definition(object_id),'[',''),']','')) > 0

The replaces get rid of brackets [] and then if you are looking for the database name plus the dot, you should find all references.

"This is important because if I accidently decommission tables that are currently in use by production class s-procedures, it will not be pretty."

I certainly hope you are planning on testing this first :) At the very least, you are demonstrating one great thing about susing all stored procedure access. Easy searching the code for references.