Combine variable and select statement

  • I want to combine this select statement and a variable(@EndUserDel) to get result.how can i do that?

    declare @EndUserDel nvarchar(20)

    select * FROM [dbo].[Service]

    i want this way select *,@EndUserDel FROM [dbo].[Service]

    how can i do that

  • vahid.arr (3/11/2013)


    I want to combine this select statement and a variable(@EndUserDel) to get result.how can i do that?

    declare @EndUserDel nvarchar(20)

    select * FROM [dbo].[Service]

    i want this way select *,@EndUserDel FROM [dbo].[Service]

    how can i do that

    So, just do what you have written.

    select *, @EndUserDel FROM [dbo].[Service]

    It is a valid T-SQL statement!

    _____________________________________________
    "The only true wisdom is in knowing you know nothing"
    "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
    (So many miracle inventions provided by MS to us...)

    How to post your question to get the best and quick help[/url]

  • Its worth noting that, if you do not assign a value to @EndUserDel it will return NULL value. See my comments in the code below

    IF OBJECT_ID('tempdb..#service') IS NOT NULL

    DROP TABLE #service;

    CREATE TABLE #service (x varchar(10));

    INSERT INTO #service SELECT 'abc' UNION ALL SELECT 'xyz';

    DECLARE @EndUserDel AS varchar(10);

    -- No value has been set for @EndUserDel so expect only NULLS for EndUserDel

    -- Note the resultset:

    SELECT *, @EndUserDel AS EndUserDel FROM #service

    -- setting value for EndUserDel

    SET @EndUserDel='Bob';

    --Now look at the resultset:

    SELECT *, @EndUserDel AS EndUserDel FROM #service

    DROP TABLE #service;

    GO

    "I cant stress enough the importance of switching from a sequential files mindset to set-based thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code."

    -- Itzik Ben-Gan 2001

  • vahid.arr (3/11/2013)


    I want to combine this select statement and a variable(@EndUserDel) to get result.how can i do that?

    declare @EndUserDel nvarchar(20)

    select * FROM [dbo].[Service]

    i want this way select *,@EndUserDel FROM [dbo].[Service]

    how can i do that

    If you're trying to put something into the variable, you'll need to assign its value in the select statement.

    declare @EndUserDel nvarchar(20);

    select @EndUserDel=<FieldName> from dbo.Service;

    The nvarchar variable is scalar, so you'll want to put some kind of filter on the query so it only returns one record too. Not sure if that's what you were trying to accomplish, but maybe this helps.

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply