0

This is my .sql file, it's saved as "Split.sql" and is located in C:\

INSERT Employee (Name)
    values ('Roland Eric Real')
SELECT *  FROM Employee
GO

select 
    Name, 
    -- instead of spaces you can use any delimiter that you have like a comma
    -- We replace the delimiter with dot, which is used by the function PARSENAME
    PARSENAME( REPLACE(Name, ' ','.') ,3) as LastName, 
    PARSENAME( REPLACE(Name, ' ','.') ,2) as FirstName, 
    PARSENAME( REPLACE(Name, ' ','.') ,1) as MiddleName
from Employee
GO

-- If you need to alter the table then
-- Alter the table and add new columns
ALTER TABLE Employee
ADD LastName varchar(50),
    FirstName varchar(50),
    MiddleName varchar(50)
GO

-- Moving the data from FullName to the new columns
UPDATE Employee SET 
    LastName   = PARSENAME( REPLACE(Name, ' ','.') ,3), 
    FirstName  = PARSENAME( REPLACE(Name, ' ','.') ,2), 
    MiddleName = PARSENAME( REPLACE(Name, ' ','.') ,1)
GO

SELECT *  FROM Employee
GO

-- Now we can drop the old column
ALTER TABLE Employee
 DROP COLUMN Name
GO

-- Reclaim space by rebuilt the table
ALTER TABLE Employee REBUILD 
GO

SELECT *  FROM Employee
GO

I am using windows form in visual studio c#. Basically, when I press a button in windows form, I want it to run that code in sql. Any help would be appreciated, thank you. ^_^

user3289917
  • 99
  • 1
  • 2
  • 7
  • Anything you have tried? – Michał Turczyn Mar 25 '18 at 10:17
  • `GO` is not a T-SQL statement. It's a batch separator recognized by SQL Server tools like SSMS and SQLCMD. In application code, the easiest method is to execute each batch individually. Otherwise, you'll need to use the SMO API (which is widely used in SQL Server tools). Note that SMO adds a dependency since that's not distributed with the .NET framework. – Dan Guzman Mar 25 '18 at 10:19
  • Refer to your same, multiple threads on msdn. – SMor Mar 25 '18 at 13:01

0 Answers0