Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Proc with table name & timestamp as parameters

Author  Topic 

gm73
Starting Member

1 Post

Posted - 2006-01-21 : 17:29:20
Hi

I've been trying to develop a function or proc that takes a table name and a timestamp as parameters and returns a Y,if there are rows in that table with a timestamp > parameter timestamp else returns N.

1. A function won't work as you are not allowed to pass a timestamp datatype as a parameter, nor can you use the table name that you pass as a parameter as you cannot EXEC a sql statement in a function ie EXEC 'SELECT * FROM' + @TableName.

2. I have tried using a proc but I have a similar problem with the EXEC.

Lastly, will I be able to use this proc in a WHERE clause ie SELECT ... WHERE dbo.procRowsExist = 'Y'

Please help as I'm struggling with this one !

PS. Some faulty code, showing roughly what I'm attempting, supplied below:

ALTER FUNCTION dbo.fnRowsExist (@TableName char(30), @TimeStamp char(16))
RETURNS CHAR(1)
AS
BEGIN
RETURN (
SELECT
CASE 'Y'
WHEN (SELECT TOP 1 'Y' FROM dbo.GtrDetail WHERE [TimeStamp] > convert(timestamp,@TimeStamp) )
THEN 'Y'
ELSE 'N'
END
)
END

--SELECT TOP 1 'Y' FROM dbo.GtrDetail WHERE [TimeStamp] > 0x00000000003F9A0F
--SELECT dbo.fnRowsExist ('GtrDetail',00000000003F9A0F)

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2006-01-21 : 21:36:31
This stored procedure code will do what you want.

You cannot use an EXECUTE in a WHERE clause.

drop proc P_CHECK_TABLE_TIMESTAMP
go
create procedure P_CHECK_TABLE_TIMESTAMP
(
@table_owner sysname,
@table_name sysname,
@column_name sysname,
@timestamp_value varbinary(16),
@timestamp_exists bit output
)
as
set nocount on

set @timestamp_exists = 0

if not exists(
select
*
from
information_schema.columns
where
TABLE_SCHEMA = @table_owner and
TABLE_NAME = @table_name and
COLUMN_NAME = @column_name
)
begin
-- Return error code -1 if table or column does not exist
return -1
end

declare @sql nvarchar(4000)

select @sql =
'
if exists(
select *
from ['+@table_owner+'].['+@table_name+']
where ['+@column_name+'] > @timestamp_in
)
begin
set @was_found = 1
end
else
begin
set @was_found = 0
end
'

execute sp_executesql
@sql,
N'@timestamp_in varbinary(16), @was_found bit output',
@timestamp_in = @timestamp_value,
@was_found = @timestamp_exists output

return 0

go


Code to execute the stored procedure:

declare @retcode int,
@table_owner sysname,
@table_name sysname,
@column_name sysname,
@timestamp_value varbinary(16),
@timestamp_exists bit

set @table_owner = 'dbo'
set @table_name = 'T_MY_TABLE'
set @column_name = 'MY_TIMESTAMP_COLUMN_NAME'
set @timestamp_value = 0x0000000000001A52

exec @retcode = P_CHECK_TABLE_TIMESTAMP
@table_owner,
@table_name,
@column_name,
@timestamp_value,
@timestamp_exists output

-- Show Output Results
select [@retcode] = @retcode,
[@timestamp_exists] = @timestamp_exists





CODO ERGO SUM
Go to Top of Page
   

- Advertisement -