Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase «««123

Insert Update Stored Procedure for a table Expand / Collapse
Author
Message
Posted Saturday, August 30, 2008 1:21 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Saturday, August 30, 2008 1:05 PM
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
Post #561652
Posted Monday, September 1, 2008 2:20 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Monday, April 14, 2014 6:47 AM
Points: 967, 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
Post #561888
Posted Friday, November 21, 2008 3:06 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Thursday, July 17, 2014 6:57 AM
Points: 55, Visits: 159
hi guys/girls

try this as an alternative as well

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

cheers

chris
Post #606382
Posted Tuesday, November 25, 2008 8:13 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Monday, April 14, 2014 6:47 AM
Points: 967, 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
Post #608383
Posted Wednesday, November 26, 2008 3:26 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, January 17, 2012 9:02 AM
Points: 2, Visits: 5
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 ' '.
Post #608926
Posted Wednesday, November 26, 2008 3:32 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Monday, April 14, 2014 6:47 AM
Points: 967, 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
Post #608929
Posted Wednesday, November 26, 2008 3:35 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, January 17, 2012 9:02 AM
Points: 2, Visits: 5
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
Post #608931
Posted Friday, January 9, 2009 12:32 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, October 22, 2013 11:14 AM
Points: 140, Visits: 312
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.
Post #633769
Posted Tuesday, May 5, 2009 10:33 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, October 10, 2014 1:26 PM
Points: 1, Visits: 35
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!!
Post #710455
Posted Tuesday, October 22, 2013 11:08 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, August 6, 2014 3:03 PM
Points: 1, Visits: 16
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,>
Post #1507248
« Prev Topic | Next Topic »

Add to briefcase «««123

Permissions Expand / Collapse