Re: use of INSERT with Dynamic SQL

From: Hari (hari_prasad_k_at_hotmail.com)
Date: 02/23/04


Date: Mon, 23 Feb 2004 08:05:04 +0530

Hi,

Pls use the below piece of code format,

declare @sql nvarchar(1000)
set @SQL =('INSERT '+@MyTable+'(ID, Name, Year) VALUES ('+@ID+', '+@Name+',
'+@Year+')')
exec sp_executesql @sql

Thanks
Hari
MCDBA

"Edi Fellmann" <anonymous@discussions.microsoft.com> wrote in message
news:24430DB5-FB10-4876-B5FB-74D43B758969@microsoft.com...
> I need to execute with INSERT to a table which name changes.
> I supose the only way is to use dynamic SQL.
> First we create the MyTable table.
> The spInsertMyTable inserts the values, no problems.
> The spInsertMyTableDynamic does not work. I send the error message.
> Is there a way to execute an INSERT statemente with dynamic SQL?
> Thanks you for any help.
>
>
> CREATE TABLE MyTable
> (
> ID int NOT NULL
> Primary Key,
> Name varchar(50) NULL,
> Year varchar(50) NULL
> )
>
> GO
>
>
> CREATE /*ALTER*/ PROCEDURE spInsertMyTable
> (
> @ID varchar(50),
> @Name varchar(50),
> @Year varchar(50)
> )
> AS
> INSERT MyTable(ID, Name, Year)
> VALUES (@ID, @Name, @Year)
> GO
>
> EXEC spInsertMyTable
>
> @ID = '13',
> @Name ='Test',
> @Year = '2004'
>
> /*
> CREATE/*ALTER*/ PROCEDURE spInsertMyTableDynamic
> (
> @ID varchar(50),
> @Name varchar(50),
> @Year varchar(50),
> @MyTable varchar(50)
> )
> AS
>
> DECLARE @SQL varchar(1000)
>
> SELECT @SQL =('INSERT '+@MyTable+'(ID, Name, Year)
> VALUES ('+@ID+', '+@Name+', '+@Year+')')
>
> PRINT @SQL
> EXEC @SQL
> --GO
>
> EXEC spInsertMyTableDynamic
> @ID = '13',
> @Name ='Test',
> @Year = '2004',
> @MyTable ='MyTable'
>
> */
> /*
> INSERT MyTable(ID, Name, Year)
> VALUES (13, Test, 2004)
> Server: Msg 2812, Level 16, State 62, Line 21
> Could not find stored procedure 'INSERT MyTable(ID, Name, Year)
> VALUES (13, Test, 2004)'.
> */



Relevant Pages

  • Re: Application role to access xp_cmdshell
    ... This necessitates that your user procs be owned ... See Cross-database chaining in the SQL 2000 ... You will also need to allow non-sysadmin users to execute xp_cmdshell. ... EXEC sp_dboption 'MyDatabase', 'db chaining', true ...
    (microsoft.public.sqlserver.security)
  • Re: Full text catalog just not populating
    ... exec sp_defaultdb N'NT Authority\System', N'master' ... means either the network guys in my company who don't know SQL but are admins ... > needs this login to log into SQL Server and you can either add back this ... >> fetching U ...
    (microsoft.public.sqlserver.fulltext)
  • Re: Help with Stored Procedure
    ... OJ's method works fine and I agree that dynamic sql has its place. ... > variable to the SQL statement they wanted to execute. ... >> You can't send a table name to an sproc as a variable. ...
    (microsoft.public.sqlserver.programming)
  • Re: Indexing delay for one row.
    ... script and WAITFOR and varying the delay from 1 to 18 seconds. ... could you confirm your exact version of SQL Server that you are seeing ... It is possible that a change was made to the pooling frequence under SP3 to ... exec sp_fulltext_table 'FTSTable','create','FTSCatalog','FTSTable_IDX' ...
    (microsoft.public.sqlserver.fulltext)
  • Re: Casting to VARCHAR(MAX)
    ... But when you do the EXEC, the statement the EXEC passes to SQL Server looks like ... So the string that gets put into #TEMP1 will be truncated to 8000 characters. ... that will force the whole expression concatenating the strings to return VARCHARand you would get the whole string inserted into the table. ...
    (microsoft.public.sqlserver.programming)

Loading