0

I am having a query shortcut that is generating create table in SQL Server Management Studio 2016. This works fine when I am not trying to select schema.table, but once I am trying to select such object I am getting error Incorrect syntax near '.'.

My query shortcut looks like:

exec sp_executesql 'huge sql oneliner', N'@table_name SYSNAME', @table_name =

Any idea how I can pass objects like that to the query shortcut?

update 1:

The only workaround I kind of found is to wrap the table name with the single quotes, which is a bit annoying.

update 2:

I simplified my shortcut a bit for debugging purposes:

exec sp_executesql N'select @table_name;', N'@table_name varchar(max)', @table_name =

and when I ran it this is what I captured in the Profiler:

exec sp_executesql N'select @table_name;', N'@table_name varchar(max)', @table_name = dbo.Categories

which makes sense for the workaround I found earlier, but still would love to make it work without surrounding it in single quotes by myself

3
  • As an aside - why do you need so many tables of identical schema that a shortcut is useful? Commented May 31, 2017 at 18:20
  • It is just an easy way for me to see the table structure and generate an alter statement if I need to change something in the table. It combines sp_help with generate "create table SQL" + find all usages of the table. I know I can use an Object Explorer, but would prefer to have a shortcut I can execute with just a key press.
    – AlexanderM
    Commented May 31, 2017 at 18:25
  • Removed my answer - I actually haven't played around much with query shortcuts, and didn't realize it was a literal thing. From the stuff I've glanced at, I don't see a way to embed the selected value in the middle of the shortcut, which is what you'd need to include the necessary single quotes without having to type them.
    – RDFozz
    Commented May 31, 2017 at 19:38

1 Answer 1

1

The result of a shortcut must still be valid SQL. As the documentation makes clear, the parameter values must be surrounded by quotes. You could use trace or Profiler to capture the actual code submitted to see exactly what's going wrong.

As the additional text seems to be appended to the shortcut rather than embedded in it there's no way to pre-wrap the additional text in quotes.

Perhaps a custom snippet would suit your needs better? Though it does take a little work to establish and you're obviously sensitive to gratuitous keystrokes.

If you're feeling very brave you could change the user's default schema inside the dynamic SQL and omit the schema from the appended text, removing the need for quotes.

1
  • even shortcut like "exec sp_executesql N'select @table_name;', N'@table_name varchar(max)', @table_name =" is failing with the same error. So the issue is how selection is getting passed to the query shortcut so I wonder if there is a way to pass everything like it is surrounded by the single quote
    – AlexanderM
    Commented May 31, 2017 at 18:09

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.