Using MS SQL2000 at present if that makes any difference.
Is there a better way than the method below to be able to programatically access a table ?
declare @tableName as varchar(50)
declare @sql varchar(4000)
set @tableName = 'User'
print @tableName
If EXISTS(
select TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE table_name = ''+@TableName+''
)
BEGIN
set @sql = 'select * from [' + @tableName + ']'
exec(@sql)
end
Essentially Im trying to create a simple Mapping tool for CRUD operations so that I need only one Sproc for each operation, and I can pass in my parameterised object, a table name and let the database do the rest. This is purely for my own personal education, hence why Im not using an established framework, so if there are any major gotcha’s with my idea or the code above, I’d appreciate knowing as well.
Thanks
This is complete example to create a SP by follow your initial code:
This SP always return a recordset so you can check
the value of the field FOUNDto know if the table exist or notusage:
Hope it helps