When writing a select statement with '*' (for example select *
(...)), I'd like to use the keyboard combination [shift+2] +[2], instead of [shift+8]. This is because I can find the right shift and 2 button easier on my keyboard than the left shift and 8. Though it may look arbitrary to you, for me it isn't and the question underlying is valid anyway. At least I think it is. :)
I try to accomplish what I want by declaring a variable
with the name '@2' and I want the dbms to interpret that as '*' and not as a 'string *'
, so as a *-command. Later I want to make this as a global variable, but for purpose of this question I ask it as whether I only want a temporary variable.
Is it possible
to have the dbms have it interpret it as a *-command? If so, how? Possibly I need to use a different datatype
, but which?
See beneath the piece of script, the result now and the desired result. I hope you can help.
Piece of script:
declare @2 nvarchar
set @2='*'
select @2 from TableAA
Result now:
(No column name)
*
*
*
etc
Desired result:
A B C
1 43 25
2 33 788
3 13 83
etc.
//It appears it can't... See the answers beneath. I did do something else to achieve something like it: I installed AutoHotkey and wrote a script that inserts a '*' when I press Left shift an Escape. Works like a charm. See that script beneath.
>+Esc::
Send, *
Return
You can't use variables names to bind columns. You have to use Dynamic SQL
in following:
DECLARE @2 NVARCHAR(10)
SET @2 = '*'
DECLARE @sql NVARCHAR(1000);
SET @sql = N'SELECT ' + @2 + ' FROM TableAA'
EXEC(@sql)