0

This is a far smaller version of a query that basically needs a variable of the table on which everything is run.

When I run the procedure I get the error message:

Msg 156, Level 15, State 1, Line 2 Incorrect syntax near the keyword 'table'.

alter procedure james_tester
@tablename nvarchar(200)
as

        BEGIN
        declare @sql nvarchar(max)

        set @sql =  

        '
        select * from ' 

        + @tablename

            EXECUTE sp_executesql @sql

        END

To fix this i tried using things like quotename and played around with the format but nothing seems to have worked yet.

execute james_tester 'dbo.Calendar table' (That is the table I am wanting to query)

2 Answers 2

1

The problem lies in how you call your procedure.

execute james_tester 'dbo.Calendar table'

should be

execute james_tester 'dbo.Calendar'

Hence the error message :

Incorrect syntax near the keyword 'table'.

Sign up to request clarification or add additional context in comments.

Comments

1

Table is a keyword of sql server. So you couldn't use it as an alias with out []. Try this

execute james_tester 'dbo.Calendar [table]'

or

execute james_tester 'dbo.Calendar t'

or

execute james_tester 'dbo.Calendar'

1 Comment

Thanks. Selected * into a table 'calendar', repeated all steps and everything worked!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.