問題:如何授予Insert的權限給所有Table
解法:使用Cursor加上INFORMATION_SCHEMA.Tables組成grant Table的SQL後,在用EXECUTE語法執行授權的SQL
--授權所有Table INSERT的權限
DECLARE @tables TABLE(ROWID int IDENTITY(1,1), SQLSTR varchar(500))
INSERT INTO @tables
SELECT 'GRANT INSERT ON ' + TABLE_NAME + ' TO sradmin'
FROM INFORMATION_SCHEMA.Tables
DECLARE @rowid int, @sqlstr varchar(500)
SET @rowid = 0 SET @sqlstr = ''
DECLARE grant_tbl_cursor CURSOR FOR SELECT ROWID, SQLSTR FROM @tables ORDER BY ROWID
OPEN grant_tbl_cursor
FETCH NEXT FROM grant_tbl_cursor INTO @rowid,@sqlstr
WHILE @@FETCH_STATUS = 0
BEGIN
EXECUTE (@sqlstr)
FETCH NEXT FROM grant_tbl_cursor
INTO @rowid,@sqlstr
END
CLOSE grant_tbl_cursor
DEALLOCATE grant_tbl_cursor
結論: SELECT、INSERT、UPDATE與DELETE都可以使用上述的SCRIPT,如果要指定某個特定的TABLE的話,只要在INFORMATION_SCHEMA.Tables加上WHERE的條件就可以了。
沒有留言:
張貼留言