declare @TableName VARCHAR(32); --数据表名
declare @DateCounter VARCHAR(32); --时间日期
declare @ID
--创建游标
--exec 后面的语句需是字符类型,所以@TableName和@DateCounter变量需是字符类型
EXEC('DECLARE myCursor CURSOR FOR SELECT ID FROM '+@TableName+' WHERE CONVERT(VARCHAR(10),DateTime,120) = CONVERT(VARCHAR(10),'''+@DateCounter+''',120)')
OPEN myCursor
FETCH NEXT FROM myCursor INTO @ID
WHILE @@FETCH_STATUS=0
BEGIN
--业务处理
FETCH NEXT FROM myCursor INTO @ID
END
CLOSE myCursor
DEALLOCATE myCursor
--游标结束