Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Whats wrong with it (dynamic sql)

Author  Topic 

mrtanvirali
Starting Member

41 Posts

Posted - 2009-04-16 : 04:06:31
i've multiple tables with same columns and I want to insert data using stored procedure, I am using dynamic SQL.

here is my procedure:


ALTER PROCEDURE [dbo].[sp]
-- Add the parameters for the stored procedure here
--@Id as int,
@TableToInsert sysname, -- required table name to insert values into it
@IdField varchar(50), -- primary

@UserId as numeric(18,0),
@ContactName as varchar(100),
@CategoryId as int,
@Name as varchar(100),
@Address as varchar(200),
@City as varchar(50),
@State as varchar(50),
@Zip as varchar(10),
@Phone as varchar(20),
@Email as varchar(100),
@PicSub as varchar(50),
@Description as varchar(255)

AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

Declare @InsertQuery as nvarchar(2000)
Declare @PickNewId as nvarchar(500)
Declare @NewID as numeric(18,0)

set @PickNewId = 'Select ISNULL(MAX('+ @IdField +'),0) + 1 from '+ @TableToInsert +''
--EXEC sp_executesql @PickNewId, N'@NewID int OUTPUT', @NewID OUTPUT

--Return if column is already identity
IF (COLUMNPROPERTY( OBJECT_ID(@TableToInsert),@IdField,'IsIdentity') = 1)
BEGIN
exec('SET IDENTITY_INSERT ' + @TableToInsert + ' ON')
END


-- Insert statements for procedure here
SET @InsertQuery = N'INSERT INTO ' + @TableToInsert +
N'(['+ @IdField +']'+
N',[ContactName] ,[CategoryId] ,[Name] ,[Address] ,[City] ,[State]'+
N',[ZipCode] ,[Phone] ,[Email] ,[PicSub] ,[Description] ,[UserId])'+
N'VALUES (exec(@PickNewId)'+
N',''+@ContactName+'' ,''+@CategoryId+'' ,''+@Name+'' ,''+@Address+'' ,''+@City+'''+
N',''+@State+'' ,''+@Zip+'' ,''+@Phone+'' ,''+@Email+'' ,''+@PicSub+'' ,''+@Description+'''+
N',''+@UserId+'')'

exec (@InsertQuery)
IF (COLUMNPROPERTY( OBJECT_ID(@TableToInsert),@IdField,'IsIdentity') = 1)
BEGIN
exec('SET IDENTITY_INSERT ' + @TableToInsert + ' OFF')
END
select IDENT_CURRENT(@TableToInsert);
END


My Errors are:

if I use 'exec(@PickNewId)' to generate the new id then
1 Incorrect syntax near the keyword 'exec'.
2 Must declare the scalar variable "@PickNewId".


and If i give value in place of 'exec(@PickNewId)' then i got this

1 Conversion failed when converting the varchar value '+@CategoryId+' to data type int

How can i use dynamic sql to insert data into multiple tables

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-04-16 : 04:39:50
You can't have an EXEC procedure as a COLUMN value.



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-04-16 : 04:39:59
[code]SET @PickNewId = ' SELECT ISNULL(MAX(' + QUOTENAME(@IdField) +'), 0) + 1
FROM ' + QUOTENAME(@TableToInsert)

SET @InsertQuery = N' INSERT ' + QUOTENAME(@TableToInsert) + '
(
'+ QUOTENAME(@IdField) + ',
[ContactName],
[CategoryId],
[Name],
[Address],
[City],
[State],
[ZipCode],
[Phone],
[Email],
[PicSub],
[Description],
[UserId]
)
SELECT (' + @PickNewId + '),
' + QUOTENAME(@ContactName, '''') + ',
' + QUOTENAME(@CategoryId, '''') + ',
' + QUOTENAME(@Name, '''') + ',
' + QUOTENAME(@Address, '''') + ',
' + QUOTENAME(@City, '''') + ',
' + QUOTENAME(@State, '''') + ',
' + QUOTENAME(@Zip, '''') + ',
' + QUOTENAME(@Phone, '''') + ',
' + QUOTENAME(@Email, '''') + ',
' + QUOTENAME(@PicSub, '''') + ',
' + QUOTENAME(@Description, '''') + ',
' + QUOTENAME(@UserId, '''' + '
)'[/code]


E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

mrtanvirali
Starting Member

41 Posts

Posted - 2009-04-16 : 05:21:54
not working,

Incorrect syntax near the keyword 'IF'



' + QUOTENAME(@PicSub, '''') + ',
' + QUOTENAME(@Description, '''') + ',
' + QUOTENAME(@UserId, '''' + '
)'

is giving me on every line that i write at the end of query
Go to Top of Page

mrtanvirali
Starting Member

41 Posts

Posted - 2009-04-16 : 06:00:57
thanks for the help its done now,
you just missed qouts of the last line

' + QUOTENAME(@UserId, '''')+''

Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-04-16 : 07:48:39
' + QUOTENAME(@UserId, '''') + '



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page
   

- Advertisement -