none
i want to to get all employee name from a table in a string in SQL RRS feed

All replies

  • -- Here employeemaster  is the table with empname as one nvarchar field

    declare @details nvarchar(4000)

    Set @details =''

    SELECT @details = @details + ', ' + empname FROM employeemaster 

    select @details 

    Tuesday, July 9, 2019 4:16 AM
  • Hi Himansi,

    Thank you for posting here.

    If you want to get all employee name form a table in a string in SQL, you could try the following code.

    select Name from Employee

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, July 9, 2019 7:54 AM
    Moderator
  • Hello,

    This is a totally revised post.

    using System;
    using System.Data.SqlClient;
    using System.Text;
    
    namespace DataAccessBasics
    {
        class Program
        {
            static void Main(string[] args)
            {
                var results = DataOperations.GetNames();
                Console.WriteLine(results);
                Console.ReadLine();
            }
        }
    
        public static class DataOperations
        {
            private static string ConnectionString =
                "Data Source=KARENS-PC;Initial Catalog=NorthWindAzure;" +
                "Integrated Security=True";
    
            public static string GetNames()
            {
                var sb = new StringBuilder();
    
                var selectStatement = "SELECT STUFF( (SELECT ',' + CategoryName  " + 
                                      "FROM dbo.Categories  FOR XML PATH('')),  1, 1, '')";
    
    
                using (var cn = new SqlConnection { ConnectionString = ConnectionString })
                {
                    using (var cmd = new SqlCommand { Connection = cn, CommandText = selectStatement })
                    {
                        cn.Open();
                        return cmd.ExecuteScalar().ToString();
                    }
                }
            }
        }
    }
    



    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange



    Tuesday, July 9, 2019 10:07 AM
    Moderator
  • i want to to get all employee name from a table in a string in SQL
    Can u show me how your're getting other data, so I can provide an example on the same technology?
    Maybe you're using EF, maybe you're using ADO, or maybe you're using Dapper.


    BP-LP 2005/2016 @ll rights reserved

    Tuesday, July 9, 2019 3:44 PM
  • Hi 

    Is your problem solved? If so, please post "Mark as answer" to the appropriate answer. So that it will help other members to find the solution quickly if they face the similar issue.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, July 17, 2019 6:30 AM
    Moderator