Pergunta

I have a bizarre syntax problem when working with user-defined table type (which I'm using in order to pass table-valued parameters to stored procedures).

This is a sample type definition:

CREATE TYPE tvp_Test AS TABLE
(
    Column1 int NOT NULL,
    Column2 int NOT NULL
)

This is a test table:

DECLARE @Test tvp_Test
INSERT INTO @Test VALUES (10,11)
INSERT INTO @Test VALUES (20,21)
INSERT INTO @Test VALUES (30,31)

These commands work fine:

SELECT * FROM @Test
SELECT Column1 FROM @Test

However, trying to fully qualify a column name gives a syntax error:

SELECT @Test.Column1 FROM @Test      -- Doesn't work

Any combination of quotes doesn't seem to help at all:

SELECT @Test.'Column1' FROM @Test    -- Doesn't work either
SELECT '@Test'.Column1 FROM @Test    -- As above
SELECT '@Test'.'Column1' FROM @Test  -- Still no luck
SELECT '@Test.Column1' FROM @Test    -- This works but selects the text '@Test.Column1'

This wouldn't seem such a terrible problem... until you start joining tables. Then it becomes a royal pain.

How can I fully qualify column names when working with user-defined table types?

Nenhuma solução correta

Outras dicas

You can alias the table variable like

SELECT A.Column1 FROM @Test A
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top