EXEC关键字除了执行存储过程,还执行字符串呀?
我还不知道有这么回事,请举个简单的例子让我明白下.谢谢.

解决方案 »

  1.   


    declare @a varchar(4000)set @a='select * from ta'exec(@a)
      

  2.   

    EXEC('SELECT getdate()')当前时间                                                   
    ------------------------------------------------------ 
    2009-08-11 08:57:04.903
      

  3.   

    EXEC('SELECT * FROM TB')
      

  4.   

    DECLARE tables_cursor CURSOR
       FOR
       SELECT name FROM sysobjects WHERE type = 'U'
    OPEN tables_cursor
    DECLARE @tablename sysname
    FETCH NEXT FROM tables_cursor INTO @tablename
    WHILE (@@FETCH_STATUS <> -1)
    BEGIN
       /* A @@FETCH_STATUS of -2 means that the row has been deleted.
       There is no need to test for this because this loop drops all
       user-defined tables.   */.
       EXEC ('DROP TABLE ' + @tablename)
       FETCH NEXT FROM tables_cursor INTO @tablename
    END
    PRINT 'All user-defined tables have been dropped from the database.'
    DEALLOCATE tables_cursor给你个例子
      

  5.   

    有Exec来执行SQL,与没有Exec来执行SQL,不都一样吗?
    比如说:
    ....
    Exec ('Select Getdate()')与
    ....
    Select GetDate()
    以上两种得到的结果不都一样吗?没有Exec来执行Select GetDate(),它照样会执行呀?
    Exec关键字来执行徐徐串到底有什么意义呢?