Showing posts with label asdeclare. Show all posts
Showing posts with label asdeclare. Show all posts

Friday, March 9, 2012

Help variable in SP

Dear All


CREATE PROCEDURE test AS
declare @.tableName varchar(100)
set @.tableName='ABCD'


INSERT INTO @.tableName
(A, B, C, E, F,G,H)
SELECT
A, B, C, D,
COUNT(*) AS G,
CONVERT(varchar(10), RecDate, 121), Left(TransID, 2) AS J
FROM
dbo.EFG
GROUP BY

ORDER BY
XXXX

( error INSERT INTO @.tableName, Do not find @.tableName, must be declare.)

help thanks

William


It appears that you first create @.TableName as a scalar variable, and then you attempt to insert into it as though it were a table variable.

If that properly reflects your intentions, I suggest that you first declare @.TableName as a table variable, with the columns properly defined -then your insert should work.

|||

You are trying to insert the values on the table which is given on the runtime. Typically it is a dynamic insert query.

Use the following query..

Code Snippet

CREATE PROCEDURE test AS

Declare @.tableName varchar(100)

Set @.tableName='ABCD'

Exec ('INSERT INTO ' + @.tableName + '(A, B, C, E, F,G,H)

SELECT

A, B, C, D,

COUNT(*) AS G,

CONVERT(varchar(10), RecDate, 121), Left(TransID, 2) AS J

FROM

dbo.EFG

GROUP BY XXXX

ORDER BY XXXX');

|||How to do?|||Thanks all|||

CREATE PROCEDURE test AS
declare @.tableName varchar(100)
set @.tableName='ABCD'


INSERT INTO @.tableName
(A, B, C, E, F,G,H)
SELECT
A, B, C, D,
COUNT(*) AS G,
CONVERT(varchar(10), RecDate, 121), Left(TransID, 2) AS J
FROM
dbo.EFG
GROUP BY

ORDER BY
XXXX

NOTE: You MUST supply the correct datatypes below. This is a suggested format to DECLARE a table variable and then populate that table variable.

Code Snippet


CREATE PROCEDURE dbo.MyTestProcedure
AS
BEGIN
DECLARE @.MyTable table
( ColA datatype,
ColB datatype,
ColC datatype,
ColD datatype,
ColE datatype,
ColF datatype,
ColG datatype
)

INSERT INTO @.MyTable
SELECT Col1,
Col2,
Col3,
Col4,
COUNT(*),
CONVERT(varchar(10), RecDate, 121),
Left(TransID, 2)
FROM dbo.EFG
GROUP BY
ORDER BY

SELECT *
FROM @.MyTable

END