Click to See Complete Forum and Search --> : How to solve this problem?


tranphuck
07-29-2005, 11:10 PM
I create a procedure like this:

-------------------------------
CREATE PROC
@table nvarchar(20)
AS
SELECT *
FROM @table
WHERE 1=1
GO
--------------------
when i run, SQL Server reported error. How can solve this problem?

buntine
07-29-2005, 11:52 PM
Try something like this:

CREATE PROCEDURE sp_select
@table nvarchar(20)
AS
SELECT *
FROM @table
WHERE 1=1
GO

You need to name the stored procedure.

Regards.

tranphuck
07-30-2005, 05:03 AM
Of course, my procedure has a name, but i forgot to write in my topic. With a name it still reported error.

buntine
07-30-2005, 07:23 AM
I would recommend you write what you actually have. I cannot guess.

Exactly what is the error message?

Regards.

tranphuck
07-31-2005, 11:13 PM
I would recommend you write what you actually have. I cannot guess.

Exactly what is the error message?

Regards.


Sorry, this is my Procedure:

---------
CREATE PROC getBookById
@table nvarchar(20)
AS
SELECT *
FROM @table
WHERE 1=1
GO
----------
And here is the error message:

--------
Server: Msg 137, Level 15, State 2, Procedure getBookById, Line 6
Must declare the variable '@table'.
---------

I tried declaring the '@table' variable, but it stills report error.

buntine
08-01-2005, 12:38 AM
I cannot see any glaring errors. Two suggestions, which may or may not work.

1. Instead of using @table, use something like @myTable. table may be a reserved word.

2. Try setting an alias for your table.

CREATE PROC getBookById
@myTable nvarchar(20)
AS
SELECT * FROM @myTable tbl
GO

Also, the WHERE clause you were using was pointless. Why did you have it in there?

Regards.