SQL Server SSMS Snippet - Use selection of SurroundsWith multiple times

mepcadol  于 2023-08-02  发布在  其他
关注(0)|答案(2)|浏览(99)

I'm writing an SSMS SurroundsWith snippet. I can make a selection and surround the selection with the snippet by using $selected$ as intended. I want to use it to surround a field name and use that field name multiple times in the snippet. If I use $selected$ more than once, it only pastes the field into the final $selected$. The other times are empty. Is there a way to use $selected$ multiple times in an SSMS snippet?

An example would be code to capitalize the first letter of a field.

UPPER(LEFT(word,1))+LOWER(SUBSTRING(word,2,LEN(word))) AS NewName

In this example, I would like to be able to highlight "word", select my SurroundsWith snippet, and have this snippet populated in this way:

UPPER(LEFT($selected$,1))+LOWER(SUBSTRING($selected$,2,LEN($selected$))) AS $end$

I could insert a snippet and then manually add the field three times, but it would be convenient if I could create a snippet that adds the highlighted field as many times as the snippet dictates. My hands would never have to leave the keyboard and the cursor is placed and ready for the new name of the field.

yeotifhr

yeotifhr1#

The intent of SurroundsWith is that the snippet text would surround exactly the text you have $selected$ in the query editor. Since you can only have one piece of text selected, it doesn't make sense to support more than one such token.

I get that you have a different use case than what they intended, but that doesn't make their intention odd. It just means they didn't anticipate your use case (and would probably say that it is better suited for Expansion anyway).

So, for now, I recommend you use Expansion and give up the idea that you can highlight text first (since neither snippet type will do everything you want). It means you will just have to insert the snippet with multiple tokens, type the word you wanted to be $selected$ once, then fill in the rest of the tokens. Not ideal but not that much harder than your goal.

You can submit feedback explaining your use case and request an additional type of snippet (or an expansion, no pun intended, to the SurroundsWith functionality):

jmp7cifd

jmp7cifd2#

My snippet surrounds the column name with an ODBC TRIM function, and I wanted to repeat the column name as the alias.

The hack was to create a Surround-With snippet, but copy the selected column I wanted to surround twice, and then paste it back in the Alias placeholder.

<![CDATA[{ fn TRIM( $selected$ $end$ ) } $Alias$ ]]>

So select the column name (ctrl-w),

(ctrl-c) copy it,

ctrl-k, ctrl-s the snippet,

then (ctrl-v) paste the column name in the $Alias.

Now if I could get SSMS to stop asking to save the sproc edits when I close it... >=(

相关问题