Showing posts with label figure. Show all posts
Showing posts with label figure. Show all posts

Tuesday, March 27, 2012

Combining records/Foreach Loop

I'm working on a data migration that requires combining rows/values from one table to update rows in another table, and I can't figure out if I need to do a nested FOREACH or something else. Here's the example.

I have a table called Health that has a unique child record, key is childID.

I have another table called Concerns that has multiple records for each child. The Concerns table structure has several Boolean fields that need to capture and retain a true value, no matter what the value is in the next record, i.e. once a field is true, it's always true. Then those values need to update the child record in the Health table.

So if the Concerns table has the following records for a child:

ChildID, DentalConcern, VisionConcern, HearingConcern.

1, True, False, False

1, False, True, False

1, False, False, False

The final values I need to update the Health table are:

1, True, True, False.

And of course, my recordset of Concerns has records for many children.

O.K., that's the background. I have Foreach Loop container set up to enumerate through the ADO recordset of the Concerns table. I have recordset variables set up for childID and each of the boolean Concerns fields. My thought was then to do a nested Foreach Loop container on the childID variable, with a Script Task to read in the recordset variables, then collect the True/False values in my readwrite variables I set up to "collect" the values of each record.

I think then I can compare the incoming recordset childID with the readwrite childID variable to see if it's changed, and if it has then I want to do the SQL update to the Health table. I'm stuck trying to figure out where to put my Execute SQL task to update the child record when I'm finished with one child. in the the Script Task. If it's in the nested Foreach, won't it execute the SQL for every record? Same question on the outer Foreach that's looping through the entire ADO recordset.

So should I put the Update sql statement in the Script Task instead of a separate Execute SQL Task?

Or is there a totally different way I need to look at looping through the entire recordset but doing processing on a subset based on the childID value?

Hope that makes sense, and thanks in advance for any help/suggestions.

Chera

cboom wrote:

I'm working on a data migration that requires combining rows/values from one table to update rows in another table, and I can't figure out if I need to do a nested FOREACH or something else. Here's the example.

I have a table called Health that has a unique child record, key is childID.

I have another table called Concerns that has multiple records for each child. The Concerns table structure has several Boolean fields that need to capture and retain a true value, no matter what the value is in the next record, i.e. once a field is true, it's always true. Then those values need to update the child record in the Health table.

So if the Concerns table has the following records for a child:

ChildID, DentalConcern, VisionConcern, HearingConcern.

1, True, False, False

1, False, True, False

1, False, False, False

The final values I need to update the Health table are:

1, True, True, False.

And of course, my recordset of Concerns has records for many children.

O.K., that's the background. I have Foreach Loop container set up to enumerate through the ADO recordset of the Concerns table. I have recordset variables set up for childID and each of the boolean Concerns fields. My thought was then to do a nested Foreach Loop container on the childID variable, with a Script Task to read in the recordset variables, then collect the True/False values in my readwrite variables I set up to "collect" the values of each record.

I think then I can compare the incoming recordset childID with the readwrite childID variable to see if it's changed, and if it has then I want to do the SQL update to the Health table. I'm stuck trying to figure out where to put my Execute SQL task to update the child record when I'm finished with one child. in the the Script Task. If it's in the nested Foreach, won't it execute the SQL for every record? Same question on the outer Foreach that's looping through the entire ADO recordset.

So should I put the Update sql statement in the Script Task instead of a separate Execute SQL Task?

Or is there a totally different way I need to look at looping through the entire recordset but doing processing on a subset based on the childID value?

Hope that makes sense, and thanks in advance for any help/suggestions.

Chera

Won't the following work:

UPDATE h

SET h.DentalConcern = c.MaxDentalConcern,

h.VisionConcern = c.MaxVisionConcern,

c.HearingConcern = c.MaxHearingConcern

FROM Health h

INNER JOIN (

SELECT ChildID,

CAST(MAX(CAST(DentalConcern as tinyint)) AS bit) as MaxDentalConcern,

CAST(MAX(CAST(VisionConcern as tinyint)) AS bit) as MaxVisionConcern,

CAST(MAX(CAST(HearingConcern as tinyint)) AS bit) as MaxHearingConcern,

FROM concerns

GROUP BY ChildID

) c

ON h.ChildID = c.ChildID

?

-Jamie

|||

Well, back to basic Transact-SQL for me. Did play with doing Max on the boolean fields which obviously didn't work, and didn't even think to Cast to integer. Many, many thanks.

Chera

Thursday, February 16, 2012

column dependency search

I posted this yesterday with no luck. I thought Id repost but be a bit more
descriptive. I want to be able to figure out where a column is explicitly
being called.
create proc myproc
as
select MyColumn from Table1
create view myview
as
select * from Table1
In the above scenario, I could write a query to search for MyColumn.
Therefore, MyProc would be returned in the results, but not MyView as it
didn't specifically name the MyColumn column. Is there a way?
TIA,
ChrisRtry using sp_depends [object name]
"ChrisR" wrote:

> I posted this yesterday with no luck. I thought Id repost but be a bit mor
e
> descriptive. I want to be able to figure out where a column is explicitly
> being called.
> create proc myproc
> as
> select MyColumn from Table1
> create view myview
> as
> select * from Table1
> In the above scenario, I could write a query to search for MyColumn.
> Therefore, MyProc would be returned in the results, but not MyView as it
> didn't specifically name the MyColumn column. Is there a way?
> --
> TIA,
> ChrisR|||You would have to add logic to look for tables with an * in the select list.
I am not sure how to do that but just another of many good reasons whey you
should never use * in production code.
Andrew J. Kelly SQL MVP
"ChrisR" <ChrisR@.discussions.microsoft.com> wrote in message
news:4B53BD66-CA89-4C7D-AF7D-CC9D55D6969F@.microsoft.com...
>I posted this yesterday with no luck. I thought Id repost but be a bit more
> descriptive. I want to be able to figure out where a column is explicitly
> being called.
> create proc myproc
> as
> select MyColumn from Table1
> create view myview
> as
> select * from Table1
> In the above scenario, I could write a query to search for MyColumn.
> Therefore, MyProc would be returned in the results, but not MyView as it
> didn't specifically name the MyColumn column. Is there a way?
> --
> TIA,
> ChrisR|||sp_depends is for a whole table, not a column.
--
TIA,
ChrisR
"FredG" wrote:
[vbcol=seagreen]
> try using sp_depends [object name]
> "ChrisR" wrote:
>|||I want to ignore tables with an *.
TIA,
ChrisR
"Andrew J. Kelly" wrote:

> You would have to add logic to look for tables with an * in the select lis
t.
> I am not sure how to do that but just another of many good reasons whey yo
u
> should never use * in production code.
> --
> Andrew J. Kelly SQL MVP
>
> "ChrisR" <ChrisR@.discussions.microsoft.com> wrote in message
> news:4B53BD66-CA89-4C7D-AF7D-CC9D55D6969F@.microsoft.com...
>
>|||Chris,
You have me confused then as to what you are asking. Are you looking for
sample code for the query to execute to find the column? If so then how
about this:
CREATE PROCEDURE find_text
@.Search VARCHAR(200)
-- Search the stored procedures and Scheduled Jobs for the text string
passed
-- and show which objects it is found.
AS
SET NOCOUNT ON
SET @.Search = '%' + @.Search + '%'
-- Create a table var to store each part
DECLARE @.tmpSearch TABLE ([Object Name] VARCHAR(50),[Sub Level]
VARCHAR(40),[Object Type] VARCHAR(24))
INSERT INTO @.tmpSearch
SELECT DISTINCT a.Name,SPACE(40) AS 'Sub Level',
CASE WHEN a.xType = 'C' THEN 'Check Constraint'
WHEN a.xType = 'D' THEN 'Default constraint'
WHEN a.xType = 'F' THEN 'FK constraint'
WHEN a.xType = 'L' THEN 'Log'
WHEN a.xType = 'FN' THEN 'Scaler Function'
WHEN a.xType = 'IF' THEN 'Inline Function'
WHEN a.xType = 'P' THEN 'Stored Procedure'
WHEN a.xType = 'PK' THEN 'PK constraint'
WHEN a.xType = 'RF' THEN 'Replication Filer'
WHEN a.xType = 'S' THEN 'System Table'
WHEN a.xType = 'TF' THEN 'Table Function'
WHEN a.xType = 'TR' THEN 'Trigger'
WHEN a.xType = 'U' THEN 'User Table'
WHEN a.xType = 'UQ' THEN 'Unique constraint'
WHEN a.xType = 'V' THEN 'View'
WHEN a.xType = 'X' THEN 'Extended SP'
ELSE 'Other' END AS Object_Type
FROM dbo.sysObjects AS a WITH (NOLOCK)
INNER JOIN dbo.syscomments AS b WITH (NOLOCK)
ON a.ID = b.ID
WHERE ENCRYPTED = 0
AND Text LIKE @.Search
INSERT INTO @.tmpSearch
SELECT A.[Name],B.Step_Name,'Scheduled Job' AS Object_Type
FROM MSDB.dbo.SYSJOBS AS A WITH (NOLOCK) INNER JOIN
MSDB.dbo.SYSJOBSTEPS AS B WITH (NOLOCK)
ON A.JOB_ID = B.JOB_ID
WHERE B.Command LIKE @.Search
SELECT [Object Name],[Object Type],[Sub Level]
FROM @.tmpSearch
ORDER BY [Object Type],[Object Name]
Andrew J. Kelly SQL MVP
"ChrisR" <ChrisR@.discussions.microsoft.com> wrote in message
news:9744BE53-07DF-4427-96F5-D0C10EFBD388@.microsoft.com...[vbcol=seagreen]
>I want to ignore tables with an *.
> --
> TIA,
> ChrisR
>
> "Andrew J. Kelly" wrote:
>|||You can search syscomments.
Josh
declare @.keyword varchar(32)
set @.keyword = 'MyColumn'
select
o.[id] as sid,
o.[name] as procname,
c.colid,
cast(c.[text] as varchar(4000)) as proctext
into #mythingy
from sysobjects o
inner join syscomments c
on o.id=c.id
where xtype in ('p','if')
and left(o.[name],3) not in ('dmf','dt_')
--
and charindex(@.keyword ,c.[text])>1
order by o.[name], c.colid
----
select * from #mythingy
----
-- and then, for a few pennies more, you can parse the lines:
set nocount on
declare @.jx int, @.cmd varchar(255), @.t char(1), @.lf char(1), @.lx int
declare @.sid int, @.procname varchar(255), @.colid int, @.proctext
varchar(4000)
declare @.keyword varchar(32)
set @.keyword = 'MyColumn'
declare @.myhits table
(
sid int,
procname varchar(255),
colid int,
jx int,
cmd varchar(4000)
)
declare ix cursor for
select sid, procname, colid, proctext from #mythingy
----
set @.t = char(9)
set @.lf = char(10)
OPEN ix
FETCH NEXT FROM ix into @.sid, @.procname, @.colid, @.proctext
WHILE @.@.FETCH_STATUS = 0
BEGIN
set @.jx = charindex(@.keyword,@.proctext)
while @.jx > 0
begin
set @.cmd = substring(@.proctext,@.jx,60)
set @.lx = charindex(@.lf,@.cmd)
if @.lx > 0
set @.cmd = left(@.cmd,@.lx-1)
set @.cmd = replace(@.cmd,@.t,' ')
print str(@.sid) + ' ' + @.procname + ' ' + str(@.colid) + ' ' +
str(@.jx) + ' ' + @.cmd
insert into @.myhits
values (@.sid, @.procname, @.colid, @.jx, @.cmd)
set @.jx = @.jx + 8
set @.jx = charindex(@.keyword,@.proctext, @.jx)
end
FETCH NEXT FROM ix into @.sid, @.procname, @.colid, @.proctext
print ''
END
CLOSE ix
DEALLOCATE ix
select *
from @.myhits
order by cmd, procname, colid, jx

On Wed, 21 Dec 2005 07:45:03 -0800, ChrisR
<ChrisR@.discussions.microsoft.com> wrote:
>I posted this yesterday with no luck. I thought Id repost but be a bit more
>descriptive. I want to be able to figure out where a column is explicitly
>being called.
>create proc myproc
>as
>select MyColumn from Table1
>create view myview
>as
>select * from Table1
>In the above scenario, I could write a query to search for MyColumn.
>Therefore, MyProc would be returned in the results, but not MyView as it
>didn't specifically name the MyColumn column. Is there a way?

column dependency search

I posted this yesterday with no luck. I thought Id repost but be a bit more
descriptive. I want to be able to figure out where a column is explicitly
being called.
create proc myproc
as
select MyColumn from Table1
create view myview
as
select * from Table1
In the above scenario, I could write a query to search for MyColumn.
Therefore, MyProc would be returned in the results, but not MyView as it
didn't specifically name the MyColumn column. Is there a way?
TIA,
ChrisR
try using sp_depends [object name]
"ChrisR" wrote:

> I posted this yesterday with no luck. I thought Id repost but be a bit more
> descriptive. I want to be able to figure out where a column is explicitly
> being called.
> create proc myproc
> as
> select MyColumn from Table1
> create view myview
> as
> select * from Table1
> In the above scenario, I could write a query to search for MyColumn.
> Therefore, MyProc would be returned in the results, but not MyView as it
> didn't specifically name the MyColumn column. Is there a way?
> --
> TIA,
> ChrisR
|||You would have to add logic to look for tables with an * in the select list.
I am not sure how to do that but just another of many good reasons whey you
should never use * in production code.
Andrew J. Kelly SQL MVP
"ChrisR" <ChrisR@.discussions.microsoft.com> wrote in message
news:4B53BD66-CA89-4C7D-AF7D-CC9D55D6969F@.microsoft.com...
>I posted this yesterday with no luck. I thought Id repost but be a bit more
> descriptive. I want to be able to figure out where a column is explicitly
> being called.
> create proc myproc
> as
> select MyColumn from Table1
> create view myview
> as
> select * from Table1
> In the above scenario, I could write a query to search for MyColumn.
> Therefore, MyProc would be returned in the results, but not MyView as it
> didn't specifically name the MyColumn column. Is there a way?
> --
> TIA,
> ChrisR
|||sp_depends is for a whole table, not a column.
TIA,
ChrisR
"FredG" wrote:
[vbcol=seagreen]
> try using sp_depends [object name]
> "ChrisR" wrote:
|||I want to ignore tables with an *.
TIA,
ChrisR
"Andrew J. Kelly" wrote:

> You would have to add logic to look for tables with an * in the select list.
> I am not sure how to do that but just another of many good reasons whey you
> should never use * in production code.
> --
> Andrew J. Kelly SQL MVP
>
> "ChrisR" <ChrisR@.discussions.microsoft.com> wrote in message
> news:4B53BD66-CA89-4C7D-AF7D-CC9D55D6969F@.microsoft.com...
>
>
|||Chris,
You have me confused then as to what you are asking. Are you looking for
sample code for the query to execute to find the column? If so then how
about this:
CREATE PROCEDURE find_text
@.Search VARCHAR(200)
-- Search the stored procedures and Scheduled Jobs for the text string
passed
-- and show which objects it is found.
AS
SET NOCOUNT ON
SET @.Search = '%' + @.Search + '%'
-- Create a table var to store each part
DECLARE @.tmpSearch TABLE ([Object Name] VARCHAR(50),[Sub Level]
VARCHAR(40),[Object Type] VARCHAR(24))
INSERT INTO @.tmpSearch
SELECT DISTINCT a.Name,SPACE(40) AS 'Sub Level',
CASE WHEN a.xType = 'C' THEN 'Check Constraint'
WHEN a.xType = 'D' THEN 'Default constraint'
WHEN a.xType = 'F' THEN 'FK constraint'
WHEN a.xType = 'L' THEN 'Log'
WHEN a.xType = 'FN' THEN 'Scaler Function'
WHEN a.xType = 'IF' THEN 'Inline Function'
WHEN a.xType = 'P' THEN 'Stored Procedure'
WHEN a.xType = 'PK' THEN 'PK constraint'
WHEN a.xType = 'RF' THEN 'Replication Filer'
WHEN a.xType = 'S' THEN 'System Table'
WHEN a.xType = 'TF' THEN 'Table Function'
WHEN a.xType = 'TR' THEN 'Trigger'
WHEN a.xType = 'U' THEN 'User Table'
WHEN a.xType = 'UQ' THEN 'Unique constraint'
WHEN a.xType = 'V' THEN 'View'
WHEN a.xType = 'X' THEN 'Extended SP'
ELSE 'Other' END AS Object_Type
FROM dbo.sysObjects AS a WITH (NOLOCK)
INNER JOIN dbo.syscomments AS b WITH (NOLOCK)
ON a.ID = b.ID
WHERE ENCRYPTED = 0
AND Text LIKE @.Search
INSERT INTO @.tmpSearch
SELECT A.[Name],B.Step_Name,'Scheduled Job' AS Object_Type
FROM MSDB.dbo.SYSJOBS AS A WITH (NOLOCK) INNER JOIN
MSDB.dbo.SYSJOBSTEPS AS B WITH (NOLOCK)
ON A.JOB_ID = B.JOB_ID
WHERE B.Command LIKE @.Search
SELECT [Object Name],[Object Type],[Sub Level]
FROM @.tmpSearch
ORDER BY [Object Type],[Object Name]
Andrew J. Kelly SQL MVP
"ChrisR" <ChrisR@.discussions.microsoft.com> wrote in message
news:9744BE53-07DF-4427-96F5-D0C10EFBD388@.microsoft.com...[vbcol=seagreen]
>I want to ignore tables with an *.
> --
> TIA,
> ChrisR
>
> "Andrew J. Kelly" wrote:
|||You can search syscomments.
Josh
declare @.keyword varchar(32)
set @.keyword = 'MyColumn'
select
o.[id] as sid,
o.[name] as procname,
c.colid,
cast(c.[text] as varchar(4000)) as proctext
into #mythingy
from sysobjects o
inner join syscomments c
on o.id=c.id
where xtype in ('p','if')
and left(o.[name],3) not in ('dmf','dt_')
and charindex(@.keyword ,c.[text])>1
order by o.[name], c.colid
select * from #mythingy
-- and then, for a few pennies more, you can parse the lines:
set nocount on
declare @.jx int, @.cmd varchar(255), @.t char(1), @.lf char(1), @.lx int
declare @.sid int, @.procname varchar(255), @.colid int, @.proctext
varchar(4000)
declare @.keyword varchar(32)
set @.keyword = 'MyColumn'
declare @.myhits table
(
sidint,
procnamevarchar(255),
colidint,
jxint,
cmdvarchar(4000)
)
declare ix cursor for
select sid, procname, colid, proctext from #mythingy
set @.t = char(9)
set @.lf = char(10)
OPEN ix
FETCH NEXT FROM ix into @.sid, @.procname, @.colid, @.proctext
WHILE @.@.FETCH_STATUS = 0
BEGIN
set @.jx = charindex(@.keyword,@.proctext)
while @.jx > 0
begin
set @.cmd = substring(@.proctext,@.jx,60)
set @.lx = charindex(@.lf,@.cmd)
if @.lx > 0
set @.cmd = left(@.cmd,@.lx-1)
set @.cmd = replace(@.cmd,@.t,' ')
print str(@.sid) + ' ' + @.procname + ' ' + str(@.colid) + ' ' +
str(@.jx) + ' ' + @.cmd
insert into @.myhits
values (@.sid, @.procname, @.colid, @.jx, @.cmd)
set @.jx = @.jx + 8
set @.jx = charindex(@.keyword,@.proctext, @.jx)
end
FETCH NEXT FROM ix into @.sid, @.procname, @.colid, @.proctext
print ''
END
CLOSE ix
DEALLOCATE ix
select *
from @.myhits
order by cmd, procname, colid, jx
On Wed, 21 Dec 2005 07:45:03 -0800, ChrisR
<ChrisR@.discussions.microsoft.com> wrote:
>I posted this yesterday with no luck. I thought Id repost but be a bit more
>descriptive. I want to be able to figure out where a column is explicitly
>being called.
>create proc myproc
>as
>select MyColumn from Table1
>create view myview
>as
>select * from Table1
>In the above scenario, I could write a query to search for MyColumn.
>Therefore, MyProc would be returned in the results, but not MyView as it
>didn't specifically name the MyColumn column. Is there a way?

column dependency search

I posted this yesterday with no luck. I thought Id repost but be a bit more
descriptive. I want to be able to figure out where a column is explicitly
being called.
create proc myproc
as
select MyColumn from Table1
create view myview
as
select * from Table1
In the above scenario, I could write a query to search for MyColumn.
Therefore, MyProc would be returned in the results, but not MyView as it
didn't specifically name the MyColumn column. Is there a way?
--
TIA,
ChrisRtry using sp_depends [object name]
"ChrisR" wrote:
> I posted this yesterday with no luck. I thought Id repost but be a bit more
> descriptive. I want to be able to figure out where a column is explicitly
> being called.
> create proc myproc
> as
> select MyColumn from Table1
> create view myview
> as
> select * from Table1
> In the above scenario, I could write a query to search for MyColumn.
> Therefore, MyProc would be returned in the results, but not MyView as it
> didn't specifically name the MyColumn column. Is there a way?
> --
> TIA,
> ChrisR|||You would have to add logic to look for tables with an * in the select list.
I am not sure how to do that but just another of many good reasons whey you
should never use * in production code.
--
Andrew J. Kelly SQL MVP
"ChrisR" <ChrisR@.discussions.microsoft.com> wrote in message
news:4B53BD66-CA89-4C7D-AF7D-CC9D55D6969F@.microsoft.com...
>I posted this yesterday with no luck. I thought Id repost but be a bit more
> descriptive. I want to be able to figure out where a column is explicitly
> being called.
> create proc myproc
> as
> select MyColumn from Table1
> create view myview
> as
> select * from Table1
> In the above scenario, I could write a query to search for MyColumn.
> Therefore, MyProc would be returned in the results, but not MyView as it
> didn't specifically name the MyColumn column. Is there a way?
> --
> TIA,
> ChrisR|||sp_depends is for a whole table, not a column.
--
TIA,
ChrisR
"FredG" wrote:
> try using sp_depends [object name]
> "ChrisR" wrote:
> > I posted this yesterday with no luck. I thought Id repost but be a bit more
> > descriptive. I want to be able to figure out where a column is explicitly
> > being called.
> >
> > create proc myproc
> > as
> > select MyColumn from Table1
> >
> > create view myview
> > as
> > select * from Table1
> >
> > In the above scenario, I could write a query to search for MyColumn.
> > Therefore, MyProc would be returned in the results, but not MyView as it
> > didn't specifically name the MyColumn column. Is there a way?
> >
> > --
> > TIA,
> > ChrisR|||I want to ignore tables with an *.
--
TIA,
ChrisR
"Andrew J. Kelly" wrote:
> You would have to add logic to look for tables with an * in the select list.
> I am not sure how to do that but just another of many good reasons whey you
> should never use * in production code.
> --
> Andrew J. Kelly SQL MVP
>
> "ChrisR" <ChrisR@.discussions.microsoft.com> wrote in message
> news:4B53BD66-CA89-4C7D-AF7D-CC9D55D6969F@.microsoft.com...
> >I posted this yesterday with no luck. I thought Id repost but be a bit more
> > descriptive. I want to be able to figure out where a column is explicitly
> > being called.
> >
> > create proc myproc
> > as
> > select MyColumn from Table1
> >
> > create view myview
> > as
> > select * from Table1
> >
> > In the above scenario, I could write a query to search for MyColumn.
> > Therefore, MyProc would be returned in the results, but not MyView as it
> > didn't specifically name the MyColumn column. Is there a way?
> >
> > --
> > TIA,
> > ChrisR
>
>|||Chris,
You have me confused then as to what you are asking. Are you looking for
sample code for the query to execute to find the column? If so then how
about this:
CREATE PROCEDURE find_text
@.Search VARCHAR(200)
-- Search the stored procedures and Scheduled Jobs for the text string
passed
-- and show which objects it is found.
AS
SET NOCOUNT ON
SET @.Search = '%' + @.Search + '%'
-- Create a table var to store each part
DECLARE @.tmpSearch TABLE ([Object Name] VARCHAR(50),[Sub Level]
VARCHAR(40),[Object Type] VARCHAR(24))
INSERT INTO @.tmpSearch
SELECT DISTINCT a.Name,SPACE(40) AS 'Sub Level',
CASE WHEN a.xType = 'C' THEN 'Check Constraint'
WHEN a.xType = 'D' THEN 'Default constraint'
WHEN a.xType = 'F' THEN 'FK constraint'
WHEN a.xType = 'L' THEN 'Log'
WHEN a.xType = 'FN' THEN 'Scaler Function'
WHEN a.xType = 'IF' THEN 'Inline Function'
WHEN a.xType = 'P' THEN 'Stored Procedure'
WHEN a.xType = 'PK' THEN 'PK constraint'
WHEN a.xType = 'RF' THEN 'Replication Filer'
WHEN a.xType = 'S' THEN 'System Table'
WHEN a.xType = 'TF' THEN 'Table Function'
WHEN a.xType = 'TR' THEN 'Trigger'
WHEN a.xType = 'U' THEN 'User Table'
WHEN a.xType = 'UQ' THEN 'Unique constraint'
WHEN a.xType = 'V' THEN 'View'
WHEN a.xType = 'X' THEN 'Extended SP'
ELSE 'Other' END AS Object_Type
FROM dbo.sysObjects AS a WITH (NOLOCK)
INNER JOIN dbo.syscomments AS b WITH (NOLOCK)
ON a.ID = b.ID
WHERE ENCRYPTED = 0
AND Text LIKE @.Search
INSERT INTO @.tmpSearch
SELECT A.[Name],B.Step_Name,'Scheduled Job' AS Object_Type
FROM MSDB.dbo.SYSJOBS AS A WITH (NOLOCK) INNER JOIN
MSDB.dbo.SYSJOBSTEPS AS B WITH (NOLOCK)
ON A.JOB_ID = B.JOB_ID
WHERE B.Command LIKE @.Search
SELECT [Object Name],[Object Type],[Sub Level]
FROM @.tmpSearch
ORDER BY [Object Type],[Object Name]
--
Andrew J. Kelly SQL MVP
"ChrisR" <ChrisR@.discussions.microsoft.com> wrote in message
news:9744BE53-07DF-4427-96F5-D0C10EFBD388@.microsoft.com...
>I want to ignore tables with an *.
> --
> TIA,
> ChrisR
>
> "Andrew J. Kelly" wrote:
>> You would have to add logic to look for tables with an * in the select
>> list.
>> I am not sure how to do that but just another of many good reasons whey
>> you
>> should never use * in production code.
>> --
>> Andrew J. Kelly SQL MVP
>>
>> "ChrisR" <ChrisR@.discussions.microsoft.com> wrote in message
>> news:4B53BD66-CA89-4C7D-AF7D-CC9D55D6969F@.microsoft.com...
>> >I posted this yesterday with no luck. I thought Id repost but be a bit
>> >more
>> > descriptive. I want to be able to figure out where a column is
>> > explicitly
>> > being called.
>> >
>> > create proc myproc
>> > as
>> > select MyColumn from Table1
>> >
>> > create view myview
>> > as
>> > select * from Table1
>> >
>> > In the above scenario, I could write a query to search for MyColumn.
>> > Therefore, MyProc would be returned in the results, but not MyView as
>> > it
>> > didn't specifically name the MyColumn column. Is there a way?
>> >
>> > --
>> > TIA,
>> > ChrisR
>>|||You can search syscomments.
Josh
--
declare @.keyword varchar(32)
set @.keyword = 'MyColumn'
select
o.[id] as sid,
o.[name] as procname,
c.colid,
cast(c.[text] as varchar(4000)) as proctext
into #mythingy
from sysobjects o
inner join syscomments c
on o.id=c.id
where xtype in ('p','if')
and left(o.[name],3) not in ('dmf','dt_')
--
and charindex(@.keyword ,c.[text])>1
order by o.[name], c.colid
----
select * from #mythingy
----
-- and then, for a few pennies more, you can parse the lines:
set nocount on
declare @.jx int, @.cmd varchar(255), @.t char(1), @.lf char(1), @.lx int
declare @.sid int, @.procname varchar(255), @.colid int, @.proctext
varchar(4000)
declare @.keyword varchar(32)
set @.keyword = 'MyColumn'
declare @.myhits table
(
sid int,
procname varchar(255),
colid int,
jx int,
cmd varchar(4000)
)
declare ix cursor for
select sid, procname, colid, proctext from #mythingy
----
set @.t = char(9)
set @.lf = char(10)
OPEN ix
FETCH NEXT FROM ix into @.sid, @.procname, @.colid, @.proctext
WHILE @.@.FETCH_STATUS = 0
BEGIN
set @.jx = charindex(@.keyword,@.proctext)
while @.jx > 0
begin
set @.cmd = substring(@.proctext,@.jx,60)
set @.lx = charindex(@.lf,@.cmd)
if @.lx > 0
set @.cmd = left(@.cmd,@.lx-1)
set @.cmd = replace(@.cmd,@.t,' ')
print str(@.sid) + ' ' + @.procname + ' ' + str(@.colid) + ' ' +
str(@.jx) + ' ' + @.cmd
insert into @.myhits
values (@.sid, @.procname, @.colid, @.jx, @.cmd)
set @.jx = @.jx + 8
set @.jx = charindex(@.keyword,@.proctext, @.jx)
end
FETCH NEXT FROM ix into @.sid, @.procname, @.colid, @.proctext
print ''
END
CLOSE ix
DEALLOCATE ix
select *
from @.myhits
order by cmd, procname, colid, jx
On Wed, 21 Dec 2005 07:45:03 -0800, ChrisR
<ChrisR@.discussions.microsoft.com> wrote:
>I posted this yesterday with no luck. I thought Id repost but be a bit more
>descriptive. I want to be able to figure out where a column is explicitly
>being called.
>create proc myproc
>as
>select MyColumn from Table1
>create view myview
>as
>select * from Table1
>In the above scenario, I could write a query to search for MyColumn.
>Therefore, MyProc would be returned in the results, but not MyView as it
>didn't specifically name the MyColumn column. Is there a way?