Re: Can I Pass a Table Name as a Parameter?
- From: "Charles" <blank@xxxxxxxxxxx>
- Date: Fri, 22 May 2009 13:40:43 +0100
I haven't tried this, and I suspect it won't work, but wouldn't it be useful
if inserted and deleted were accesible to a UDF called from a trigger. The
trigger could call a generic function, which would be able to
SELECT * FROM inserted
and it would reference the appropriate table. All we would need then is a
'current' object that pointed to the table on which the trigger was called,
and it's job done.
Charles
"Bob McClellan" <bobmcc777@xxxxxxxxx> wrote in message
news:D2600232-2D55-4710-9C20-5A1A0281207F@xxxxxxxxxxxxxxxx
Dave,
If you know the tables that will be targeted in the
stored procedure, perhaps something like this would work..
Create
Proc SearchFromSomeTable
@tableName nvarchar(50)
as
if @tableName = 'TableA'
Begin
Select *
From TableA
End
if @tableName = 'TableB'
Begin
Select *
From TableB
End
"Charles" <blank@xxxxxxxxxxx> wrote in message
news:uc26wGt2JHA.4632@xxxxxxxxxxxxxxxxxxxxxxx
Hi Dave
Thanks for the reply. I realised after I had posted that the dynamic-SQL
solution would probably be suggested, but you have also answered the
question, I think, by saying that it is the _only_ way.
I have read Erland's article on this subject before, and take on-board
the reasons for avoiding dynamic-SQL if possible.
I have re-read his bit about "Common Cases when to (Not) Use Dynamic SQL:
SELECT * FROM @tablename", and don't entirely agree. I think there are
still occasions when, despite the fact that all tables are different
entities, the definition and purpose of the table is not unique, by
design. The only difference is that the table name and a couple of column
names differ, but the rules governing what is legal in the table are
common. In such a case it would seem much more efficient, and less
error-prone to be able to call common code in a trigger or check
constraint.
Perhaps Erland will reply and I can have the discussion with him too.
Cheers
Charles
"Dave Ballantyne" <symlink@no5p323mmer> wrote in message
news:uUOucts2JHA.1196@xxxxxxxxxxxxxxxxxxxxxxx
Basically , no. But yes with dynamic SQL
See this link for more info : http://www.sommarskog.se/dynamic_sql.html
--
Dave Ballantyne
http://sqlandthelike.blogspot.com/
Charles wrote:
Hi all
I realise this doesn't work as I have written it, but it demonstrates
what I would like to be able to do:
declare @TableName varchar(MAX)
set @TableName = 'MyTable'
select *
from @TableName
I am writing a trigger for a table insert, and want to put the actual
detail in a function. This function would be called from many triggers
because there are many tables that require this type of check.
Is there a way in which I can pass a table name (and, indeed, column
name) to a function so that the actual code is then generic?
It seems to me that this is not an uncommon thing to want to do, but I
haven't found any reference yet. I will continue to look, but if anyone
has a quick answer I would be most grateful.
TIA
Charles
.
- Follow-Ups:
- Re: Can I Pass a Table Name as a Parameter?
- From: Bob McClellan
- Re: Can I Pass a Table Name as a Parameter?
- References:
- Can I Pass a Table Name as a Parameter?
- From: Charles
- Re: Can I Pass a Table Name as a Parameter?
- From: Dave Ballantyne
- Re: Can I Pass a Table Name as a Parameter?
- From: Charles
- Re: Can I Pass a Table Name as a Parameter?
- From: Bob McClellan
- Can I Pass a Table Name as a Parameter?
- Prev by Date: Re: Can I Pass a Table Name as a Parameter?
- Next by Date: Re: Question on SMO and DMO
- Previous by thread: Re: Can I Pass a Table Name as a Parameter?
- Next by thread: Re: Can I Pass a Table Name as a Parameter?
- Index(es):
Relevant Pages
|