ALTER PROCEDURE [dbo].[wsp_Update_DateTime](@Table char(50),@TableID char(50), @DateField char(50), @TimeField char(50),@DateTimeField char(50))
-- Add the parameters for the stored procedure here
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
DECLARE @FirstID int
DECLARE @Date char(20)
DECLARE @Time char(20)
DECLARE @DT char(40)
DECLARE @DateTime datetime
DECLARE @cmd AS NVARCHAR(max)
SET @cmd = N'SELECT @FirstID = Min(' + @TableID + ') FROM ' + @Table
EXEC sp_executesql @cmd
WHILE @FirstID IS NOT NULL
BEGIN
DECLARE @cmd2 as NVARCHAR(max);
SET @cmd2 = N'SELECT @Date = ' + @DateField + ', @Time = ' + @TimeField + ' FROM ' + @Table + ' WHERE ' + @TableID + ' = ' + @Table;
EXEC sp_executesql @cmd2;
SET @DT = substring(@Date,1,4) + '-' + substring(@Date,5,2) + '-' + substring(@Date,7,2) + ' ' + substring(@Time,1,2) + ':' + substring(@Time,3,2) + ':' + substring(@Time,5,2);
SET @DateTime = convert(datetime,@DT,20);
DECLARE @cmd3 as NVARCHAR(max);
SET @cmd3 = N'UPDATE ' + @Table + ' SET ' + @DateTimeField + ' = @DateTime WHERE ' + @TableID + ' = @FirstID';
EXEC sp_executesql @cmd3;
DECLARE @cmd4 AS NVARCHAR(max)
SET @cmd4 = N'SELECT @FirstID = Min(' + @TableID + ') FROM ' + @Table + ' WHERE ' + @TableID + ' > @FirstID';
EXEC sp_executesql @cmd4
END
|