SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Insert Update Stored Procedure for a table


Insert Update Stored Procedure for a table

Author
Message
hsitas953
hsitas953
Forum Newbie
Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

Group: General Forum Members
Points: 1 Visits: 0
vblllove (12/6/2007)
I try to execute the Script but I get Msg 102, Level 15, State 1, Line 23
Incorrect syntax near ' '.
Msg 102, Level 15, State 1, Line 29
Incorrect syntax near ' '.
Msg 102, Level 15, State 1, Line 31
Incorrect syntax near ' '.
Msg 102, Level 15, State 1, Line 33
Incorrect syntax near ' '.
Msg 137, Level 15, State 1, Line 33
Must declare the scalar variable "@objid".
Msg 102, Level 15, State 1, Line 37
Incorrect syntax near ' '.
Msg 137, Level 15, State 1, Line 39
Must declare the scalar variable "@colname".
Msg 137, Level 15, State 2, Line 44
Must declare the scalar variable "@sysobj_type".
Msg 102, Level 15, State 1, Line 52
Incorrect syntax near ' '.
Msg 102, Level 15, State 1, Line 54
Incorrect syntax near ' '.
Msg 102, Level 15, State 1, Line 56
Incorrect syntax near ' '.
Msg 102, Level 15, State 1, Line 58
Incorrect syntax near ' '.
Msg 102, Level 15, State 1, Line 62
Incorrect syntax near ' '.
Msg 102, Level 15, State 1, Line 72
Incorrect syntax near ' '.
Msg 137, Level 15, State 2, Line 72
Must declare the scalar variable "@objname".
Msg 102, Level 15, State 1, Line 78
Incorrect syntax near ' '.
Msg 102, Level 15, State 1, Line 112
Incorrect syntax near ' '.
Msg 102, Level 15, State 1, Line 116
Incorrect syntax near ' '.
Msg 102, Level 15, State 1, Line 118
Incorrect syntax near ' '.
Msg 137, Level 15, State 2, Line 122
Must declare the scalar variable "@colname".
Msg 102, Level 15, State 1, Line 130
Incorrect syntax near ' '.
Msg 137, Level 15, State 2, Line 130
Must declare the scalar variable "@objname".
Msg 102, Level 15, State 1, Line 136
Incorrect syntax near ' '.
Msg 102, Level 15, State 1, Line 138
Incorrect syntax near ' '.
Msg 102, Level 15, State 1, Line 144
Incorrect syntax near ' '.
Msg 102, Level 15, State 1, Line 150
Incorrect syntax near ' '.
Msg 102, Level 15, State 1, Line 154
Incorrect syntax near ' '.
Msg 102, Level 15, State 1, Line 156
Incorrect syntax near ' '.
Msg 102, Level 15, State 1, Line 160
Incorrect syntax near ' '.
Msg 102, Level 15, State 1, Line 162
Incorrect syntax near ' '.
Msg 102, Level 15, State 1, Line 166
Incorrect syntax near ' '.
Msg 102, Level 15, State 1, Line 168
Incorrect syntax near ' '.
Msg 102, Level 15, State 1, Line 170
Incorrect syntax near ' '.
Msg 102, Level 15, State 1, Line 176
Incorrect syntax near ' '.
Msg 102, Level 15, State 1, Line 182
Incorrect syntax near ' '.
Msg 102, Level 15, State 1, Line 188
Incorrect syntax near ' '.
Msg 102, Level 15, State 1, Line 194
Incorrect syntax near ' '.
Msg 102, Level 15, State 1, Line 198
Incorrect syntax near ' '.
Msg 102, Level 15, State 1, Line 204
Incorrect syntax near ' '.
Msg 137, Level 15, State 2, Line 208
Must declare the scalar variable "@colname".
Msg 102, Level 15, State 1, Line 226
Incorrect syntax near ' '.
Msg 102, Level 15, State 1, Line 230
Incorrect syntax near ' '.
Msg 102, Level 15, State 1, Line 232
Incorrect syntax near ' '.
Msg 137, Level 15, State 2, Line 232
Must declare the scalar variable "@objname".
Msg 102, Level 15, State 1, Line 240
Incorrect syntax near ' '.
Msg 102, Level 15, State 1, Line 242
Incorrect syntax near ' '.
Msg 102, Level 15, State 1, Line 248
Incorrect syntax near ' '.
Msg 102, Level 15, State 1, Line 254
Incorrect syntax near ' '.
Msg 102, Level 15, State 1, Line 260
Incorrect syntax near ' '.
Msg 102, Level 15, State 1, Line 266
Incorrect syntax near ' '.
Msg 102, Level 15, State 1, Line 270
Incorrect syntax near ' '.
Msg 102, Level 15, State 1, Line 272
Incorrect syntax near ' '.
Msg 137, Level 15, State 2, Line 274
Must declare the scalar variable "@colname".
Msg 102, Level 15, State 1, Line 288
Incorrect syntax near 'end'.
a lot of errors

Prasad Bhogadi
Prasad Bhogadi
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2373 Visits: 450
Are you using the initial script or the modified script? Would you mind posting your table structure please.

Thanks

Prasad Bhogadi
www.inforaise.com
kodracon
kodracon
SSC-Enthusiastic
SSC-Enthusiastic (165 reputation)SSC-Enthusiastic (165 reputation)SSC-Enthusiastic (165 reputation)SSC-Enthusiastic (165 reputation)SSC-Enthusiastic (165 reputation)SSC-Enthusiastic (165 reputation)SSC-Enthusiastic (165 reputation)SSC-Enthusiastic (165 reputation)

Group: General Forum Members
Points: 165 Visits: 162
hi guys/girls

try this as an alternative as well

http://www.sqlservercentral.com/scripts/AutoGenerate++++++parameters/64760/

cheers

chris
Prasad Bhogadi
Prasad Bhogadi
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2373 Visits: 450
Prasad Bhogadi (9/1/2008)
Are you using the initial script or the modified script? Would you mind posting your table structure please.

Thanks


Hello hsitas953

I found that when I copy and paste the code in a text editor like Editplus the tabs that are in the script are converted to special characters "?". So you may want to paste the code in a text editor remove this characters and run from the query analyzer. I believe I have not taking enough precautions while posting the script to avoid such problems.

Thanks

Prasad Bhogadi
www.inforaise.com
nigelevenden
nigelevenden
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 11
Hi, Please forgive me sounding silly but I am a bit of a novice...
I am getting the follwoing errors. Are these as a result of my copying and pasting the script..? If so it might take me an age to try and debug as I can't see anything wrong with the transact SQL. Any tips please..?

Msg 102, Level 15, State 1, Line 22
Incorrect syntax near ' '.
Msg 102, Level 15, State 1, Line 27
Incorrect syntax near ' '.
Msg 102, Level 15, State 1, Line 28
Incorrect syntax near ' '.
Msg 102, Level 15, State 1, Line 29
Incorrect syntax near ' '.
Msg 137, Level 15, State 1, Line 29
Must declare the scalar variable "@objid".
Msg 102, Level 15, State 1, Line 31
Incorrect syntax near ' '.
Prasad Bhogadi
Prasad Bhogadi
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2373 Visits: 450
nigelevenden (11/26/2008)
Hi, Please forgive me sounding silly but I am a bit of a novice...
I am getting the follwoing errors. Are these as a result of my copying and pasting the script..? If so it might take me an age to try and debug as I can't see anything wrong with the transact SQL. Any tips please..?

Msg 102, Level 15, State 1, Line 22
Incorrect syntax near ' '.
Msg 102, Level 15, State 1, Line 27
Incorrect syntax near ' '.
Msg 102, Level 15, State 1, Line 28
Incorrect syntax near ' '.
Msg 102, Level 15, State 1, Line 29
Incorrect syntax near ' '.
Msg 137, Level 15, State 1, Line 29
Must declare the scalar variable "@objid".
Msg 102, Level 15, State 1, Line 31
Incorrect syntax near ' '.


The issue is due to the special characters that are generated in the editor. What I would request you is to copy the script and paste it in a note pad and you just replace the special characters that are shown on each line where a tab is used and replace them. It should get compiled without any issues.

Prasad Bhogadi
www.inforaise.com
nigelevenden
nigelevenden
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 11
Ok, I've read some advice on here. I paste the script into notepad, removed all the bad characters (that are easy to spot) and now the script works... I look forward to trying it out later in the day (when I get a few minutes without customers complaining all day long...lol).

Many thanks to all :-)
Keith DuAime
Keith DuAime
SSC Veteran
SSC Veteran (248 reputation)SSC Veteran (248 reputation)SSC Veteran (248 reputation)SSC Veteran (248 reputation)SSC Veteran (248 reputation)SSC Veteran (248 reputation)SSC Veteran (248 reputation)SSC Veteran (248 reputation)

Group: General Forum Members
Points: 248 Visits: 344
I've been working on similar issues including reproducing creating table and missing columns and thought this would help a lot. It does, thank you. However there are some clitches in it. I created a table

CREATE TABLE dbo.tbl
(
bi BIGINT,
d DECIMAL (18, 2),
f FLOAT,
i INT,
m MONEY,
n NUMERIC (15, 3),
r REAL,
si SMALLINT,
sm SMALLMONEY,
ti TINYINT,
sChar CHAR(10),
sVarchar VARCHAR(100),
sMax VARCHAR (MAX),
id INT IDENTITY(1, 1)
)
GO



CREATE PROCEDURE InsUpd_tbl
@bi bigint (8),
@d decimal (18,2),
NULL
@i int ,
@m money (19,4),
@n numeric (15,3),
NULL
@si smallint ,
@sm smallmoney (10,4),
@ti tinyint (1),
@sChar char (10),
@sVarchar varchar (100),
@sMax varchar (-1),
@id int
AS BEGIN IF @id <= 0 BEGIN
INSERT INTO dbo.tbl (
bi,
d,
f,
i,
m,
n,
r,
si,
sm,
ti,
sChar,
sVarchar,
sMax
)
VALUES (
@bi,
@d,
@f,
@i,
@m,
@n,
@r,
@si,
@sm,
@ti,
@sChar,
@sVarchar,
@sMax
) SET @id = @@IDENTITY SELECT @id AS id END ELSE BEGIN

UPDATE dbo.tbl SET
bi = @bi,
d = @d,
f = @f,
i = @i,
m = @m,
n = @n,
r = @r,
si = @si,
sm = @sm,
ti = @ti,
sChar = @sChar,
sVarchar = @sVarchar,
sMax = @sMax
WHERE id= @id SELECT @id AS id END END

and there are quite a few null fields. I think the precision or scale is causing this.
I have also have written a query that does the trick and thought I'd share it here.

SET NOCOUNT ON
GO
IF OBJECT_ID('dbo.fGetColumnList') IS NOT NULL
DROP FUNCTION dbo.fGetColumnList
GO
CREATE FUNCTION dbo.fGetColumnList( @TableId INT, @VariableList BIT )
RETURNS VARCHAR(MAX)
AS
BEGIN
DECLARE @ColumnList VARCHAR(MAX)
SET @ColumnList = ''

SELECT @ColumnList = @ColumnList + CASE @VariableList WHEN 1 THEN '@' ELSE '' END + LOWER(c.NAME) + ','
FROM sys.columns c
WHERE c.[object_id] = @TableId
AND c.is_identity = 0
ORDER BY c.column_id

SET @ColumnList = SUBSTRING(@ColumnList,1,LEN(@ColumnList)-1)

RETURN @ColumnList

END
GO
IF OBJECT_ID('dbo.GenerateInsUpdateScript') IS NOT NULL
DROP PROCEDURE dbo.GenerateInsUpdateScript
GO
CREATE PROCEDURE dbo.GenerateInsUpdateScript(
@TableId INT
)AS
SET NOCOUNT ON
BEGIN
SELECT t.NAME TableName, 1 OrderBy, 0 column_id,
'IF OBJECT_ID(''pInsUpd_' + t.NAME + ''') IS NOT NULL' + CHAR(13) + CHAR(10) +
' DROP PROCEDURE pInsUpd_' + LOWER(t.NAME) + CHAR(13) + CHAR(10) +
'GO' + CHAR(13) + CHAR(10) +
'CREATE PROCEDURE pInsUpd_' + LOWER(t.NAME) + '('
FROM sys.tables t
WHERE t.OBJECT_ID = COALESCE(@TableId, t.OBJECT_ID)
AND EXISTS(SELECT * FROM sys.columns ex WHERE ex.[object_id] = t.[object_id] AND ex.is_identity = 1)
UNION ALL
SELECT t.NAME TableName, 2 OrderBy, c.column_id, ' @' + LOWER(c.NAME) + ' ' + typ.NAME +
CASE
--No Precision or scale
WHEN typ.NAME IN ('bigint','int','smallint','tinyint','bit','datetime','smalldatetime','smallmoney','money','real','text') THEN ''
--Precision
ELSE
CASE WHEN c.max_length = -1 THEN '(MAX'
ELSE
'(' + CAST(COLUMNPROPERTY(c.[object_id],c.NAME,'Precision') AS VARCHAR(10)) +
--and Scale
CASE WHEN typ.NAME IN ('decimal','numeric') THEN ',' + CAST(COLUMNPROPERTY(c.[object_id],c.NAME,'Scale') AS VARCHAR(10)) ELSE '' END
END +
')'
END +
CASE WHEN c.column_id < t.max_column_id_used THEN ','
ELSE
CHAR(13) + CHAR(10) +
')' + CHAR(13) + CHAR(10) +
'AS' + CHAR(13) + CHAR(10) +
'SET NOCOUNT ON' + CHAR(13) + CHAR(10) +
'BEGIN'
END
FROM sys.tables t
INNER JOIN sys.columns c ON c.[object_id] = t.[object_id]
INNER JOIN sys.types typ ON typ.user_type_id = c.user_type_id
WHERE t.OBJECT_ID = COALESCE(@TableId, t.OBJECT_ID)
AND EXISTS(SELECT * FROM sys.columns ex WHERE ex.[object_id] = t.[object_id] AND ex.is_identity = 1)
UNION ALL
SELECT OBJECT_NAME(c.[object_id]) TableName, 3 OrderBy, NULL column_id,
' IF @' + LOWER(c.NAME) + ' = 0'
FROM sys.tables t
INNER JOIN sys.columns c ON c.[object_id] = t.[object_id] AND c.is_identity = 1
WHERE t.OBJECT_ID = COALESCE(@TableId, t.OBJECT_ID)
UNION ALL
SELECT t.NAME TableName, 4 OrderBy, null,
' INSERT INTO ' + OBJECT_NAME(t.[object_id]) + '(' + dbo.fGetColumnList(t.OBJECT_ID, 0) + ')'
FROM sys.tables t
WHERE t.OBJECT_ID = COALESCE(@TableId, t.OBJECT_ID)
AND EXISTS(SELECT * FROM sys.columns ex WHERE ex.[object_id] = t.[object_id] AND ex.is_identity = 1)
UNION ALL
SELECT t.NAME TableName, 5 OrderBy, null,
' VALUES(' + dbo.fGetColumnList(t.OBJECT_ID, 1) + ')'
FROM sys.tables t
WHERE t.OBJECT_ID = COALESCE(@TableId, t.OBJECT_ID)
AND EXISTS(SELECT * FROM sys.columns ex WHERE ex.[object_id] = t.[object_id] AND ex.is_identity = 1)
UNION ALL
SELECT t.NAME TableName, 6 OrderBy, c.column_id,
CASE WHEN c.column_id = MinMaxColumn.MinColumn THEN ' ELSE' + CHAR(13) + CHAR(10) + ' UPDATE ' + t.[name] + ' SET' + CHAR(13) + CHAR(10) ELSE '' END +
' ' + LOWER(c.NAME) + ' = @' + LOWER(c.NAME) +
CASE WHEN c.column_id < MinMaxColumn.MaxColumn THEN ',' ELSE '' END
FROM sys.tables t
INNER JOIN sys.columns c ON c.[object_id] = t.[object_id] AND c.is_identity = 0
INNER JOIN (
SELECT c.[object_id], MIN(c.column_id) MinColumn, MAX(c.column_id) MaxColumn
FROM sys.columns c
WHERE c.is_identity = 0
GROUP BY c.[object_id]
) MinMaxColumn ON MinMaxColumn.[object_id] = c.[object_id]
WHERE t.OBJECT_ID = COALESCE(@TableId, t.OBJECT_ID)
AND EXISTS(SELECT * FROM sys.columns ex WHERE ex.[object_id] = t.[object_id] AND ex.is_identity = 1)
UNION ALL
SELECT OBJECT_NAME(c.[object_id]) TableName, 7 OrderBy, NULL column_id,
' WHERE ' + LOWER(c.NAME) + ' = @' + LOWER(c.NAME) + CHAR(13) + CHAR(10) + 'END' + CHAR(13) + CHAR(10) + 'GO' + CHAR(13) + CHAR(10)
FROM sys.tables t
INNER JOIN sys.columns c ON c.[object_id] = t.[object_id] AND c.is_identity = 1
WHERE t.OBJECT_ID = COALESCE(@TableId, t.OBJECT_ID)
AND EXISTS(SELECT * FROM sys.columns ex WHERE ex.[object_id] = t.[object_id] AND ex.is_identity = 1)
ORDER BY TableName, OrderBy, column_id
END
GO


It can be changed a bit but the reason I'm getting tablename, orderby, column_id, etc... is becuase this is part of something bigger and i use osql and bcp to get the carriage return line feeds the way I want.
Kent Roberts-146295
Kent Roberts-146295
Forum Newbie
Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

Group: General Forum Members
Points: 1 Visits: 36
It works ok but beware, the values list generated for the Insert statement do not match the order of the columns in the Insert Into portion.

Easily remedied and it still saves a bunch of typing.

Thanks!!
todd.treadway
todd.treadway
Forum Newbie
Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

Group: General Forum Members
Points: 1 Visits: 21
Using SQL 2005
Float columns are returned as NULL...
CREATE PROCEDURE InsUpd_Master_invoicedetail
@LinkID int ,
@BU varchar (8),
@orgBU varchar (8),
@DB varchar (8),
@InvoiceNo varchar (32),
@SONo nvarchar (64),
NULL
NULL
NULL
NULL

If I select Insert to new query window I get
INSERT INTO [EProg_Master].[dbo].[Master_InvoiceDetail]
([BU]
,[orgBU]
,[DB]
,[InvoiceNo]
,[SONo]
,[ComPct1]
,[ComPct2]
,[ComPct3]
,[ComPct4]
VALUES
(<BU, varchar(8),>
,<orgBU, varchar(8),>
,<DB, varchar(8),>
,<InvoiceNo, varchar(32),>
,<SONo, nvarchar(32),>
,<ComPct1, float,>
,<ComPct2, float,>
,<ComPct3, float,>
,<ComPct4, float,>
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search