Execute in sqlserver
Using EXECUTE with Stored Procedures
You do not have to specify the EXECUTE keyword when you execute stored procedures when the statement is the first one in a batch.? SQL Server system stored procedures start with the characters sp_. They are physically stored in the Resource database,but logically appear in the sys schema of every system and user-defined database. When you execute a system stored procedure,either in a batch or inside a module such as a user-defined stored procedure or function,we recommend that you qualify the stored procedure name with the sys schema name.? SQL Server system extended stored procedures start with the characters xp_,and these are contained in the dbo schema of the master database. When you execute a system extended stored procedure,we recommend that you qualify the stored procedure name with master.dbo.? When you execute a user-defined stored procedure,we recommend that you qualify the stored procedure name with a schema name. We do not recommend that you name a user-defined stored procedure with the same name as a system stored procedure. For more information about executing stored procedures,see Executing Stored Procedures (Database Engine). USE AdventureWorks2008R2; GO DECLARE tables_cursor CURSOR ? ?FOR ? ?SELECT s.name,t.name? ? ?FROM sys.objects AS t ? ?JOIN sys.schemas AS s ON s.schema_id = t.schema_id ? ?WHERE t.type = 'U'; OPEN tables_cursor; DECLARE @schemaname sysname; DECLARE @tablename sysname; FETCH NEXT FROM tables_cursor INTO @schemaname,@tablename; WHILE (@@FETCH_STATUS <> -1) BEGIN; ? ?EXECUTE ('ALTER INDEX ALL ON ' + @schemaname + '.' + @tablename + ' REBUILD;'); ? ?FETCH NEXT FROM tables_cursor INTO @schemaname,@tablename; END; PRINT 'The indexes on all tables have been rebuilt.'; CLOSE tables_cursor; DEALLOCATE tables_cursor; GO IF OBJECT_ID(N'dbo.ProcTestDefaults',N'P')IS NOT NULL ? ?DROP PROCEDURE dbo.ProcTestDefaults; GO -- Create the stored procedure. CREATE PROCEDURE dbo.ProcTestDefaults ( @p1 smallint = 42,? @p2 char(1),? @p3 varchar(8) = 'CAR') AS? ? ?SET NOCOUNT ON; ? ?SELECT @p1,@p2,@p3 ; GO exec sp_addlinkedserver? @server = 'ORACLE', @srvProduct = 'Oracle', @provider = 'OraOLEDB.Oracle', @datasrc = 'ORACLE10'; exec sp_addlinkedsrvlogin @rmtsrvname = 'ORACLE', @useself = 'false', @locallogin = null, @rmtuser = 'Scott', @rmtpassword = 'tiger'; exec sp_serveroption 'ORACLE','rpc out',true; go exec('select * from scott.emp') at ORACLE; go exec('select * from scott.emp where MGR = ?',7902) at ORACLE; go declare @v int; set @v = 7902; exec('select * from scott.emp where mgr = ?',@v) at ORACLE; go Executing a dynamically built stringThe following example shows using | ? |
|
This is a simple example for sp_executesql. The example does not contain error checking and does not include checks for business rules,such as guaranteeing that order numbers are not duplicated between tables.
? |
---|