Sunday, February 19, 2012

how to use @tablename

i need to develop a stored procedue, in ehich i have to use variable table name.. as Select * from @.tableName but i m unable to do so..
it says u need to define @.tablename

heres da code
CREATE PROCEDURE validateChildId
(
@.childId int,
@.tableName varchar(50),
@.fid int output
)
AS
(
SELECT @.fid=fid FROM @.tableName
where @.childId= childId
)

if @.@.rowcount<1
SELECT
@.fid = 0
GOyou need to write a Dynamic SQL Statement for this.

Declare @.SQL VarChar(1000)

SELECT @.SQL = 'SELECT * FROM '
SELECT @.SQL = @.SQL + @.TableName

Exec ( @.SQL).

Something like this.

Do let me know if you want the code for your scenario.

Thanks
Shankar|||exec ('Select * from ' + @.TableName)

...is, for its brevity and wanton violation of sound development principles, arguably the most concentrated example of bad code one could write.

I would no more encourage you to do this than I would recommend one brand of cigarettes over another.|||i understand its the crudest way of writing code, if there's any other method which can solve this problem, please do let me know. I can Improve my knowledge too. :-)|||I wasn't disparaging your code, which effectively solves the problem that was presented.

The solution isn't the problem. The problem is the problem.

It just isn't a good idea to develop applications with this type of functionality. It opens a gaping hole into the database that user's are free to jam anything into, intentionally or inadvertently.

Waqas needs to reconsider the design of his application.

I apologize if I inadvertently offended you.

No comments:

Post a Comment