Let's create with a table example:
create table mytable (id int identity(1,1), PersonID int, unit varchar(10))
insert into mytable values (1,'Che YYYY')
insert into mytable values (1,'Mat')
insert into mytable values (1,'Phy XXXX')
--Replace space in your column with a special character and remove it in your select statement
UPDATE mytable
SET unit=REPLACE(unit,' ','')
SELECT PersonID, REPLACE(Units,'', ' ') as Units
FROM (SELECT t1.PersonID,
Units =REPLACE( (SELECT Unit AS [data()]
FROM mytable t2
WHERE t2.PersonID = t1.PersonID
ORDER BY Unit
FOR XML PATH('')
), ' ', ',')
FROM mytable t1
GROUP BY PersonID)
t0 ;
drop table mytable
Monday, July 19, 2010
Thursday, June 10, 2010
Merge SQL tables
Let say, I have two tables:
table1:(Fields:PersonID, FirstName, LastName, Role, Department)
table2:(fields: PersonID, Unit).
and I want to create a new table with these fields:
table: ( Fields:PersonID, FirstName, LastName, Role, Department, Unit).
here is the query:
SELECT table1.*, table2.Unit
INTO new_table_name
FROM table1 inner join table2
on table1.PersonID = table2.PersonID
If table1 and table2 has one-to-many relation then first use this postto make it one-to-one relaion; otherwise it'll create multiple PersonID into new table, which you don't want.
table1:(Fields:PersonID, FirstName, LastName, Role, Department)
table2:(fields: PersonID, Unit).
and I want to create a new table with these fields:
table: ( Fields:PersonID, FirstName, LastName, Role, Department, Unit).
here is the query:
SELECT table1.*, table2.Unit
INTO new_table_name
FROM table1 inner join table2
on table1.PersonID = table2.PersonID
If table1 and table2 has one-to-many relation then first use this postto make it one-to-one relaion; otherwise it'll create multiple PersonID into new table, which you don't want.
Wednesday, May 26, 2010
Bind stored procedure data into Gridview
First, let's create a stored procedure with a name: 'Dynamic_table'
and sqlconnection 'conn' and then do the following:
SqlCommand cmd = new SqlCommand("Dynamic_table", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@tableName",
SqlDbType.VarChar).Direction = ParameterDirection.Input;
cmd.Parameters["@tableName"].Value = Session["tableValue"];
cmd.Connection.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataSourceID = string.Empty;
GridView1.DataBind();
cmd.Connection.Close();
cmd.Parameters["@tableName"].Value = Session["tableValue"];
cmd.Connection.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataSourceID = string.Empty;
GridView1.DataBind();
cmd.Connection.Close();
Happy Programming !!!
Stored Procedure to select dynamic table @ runtime
Create Proc Dynamic_table
@tableName varchar(50)
as
begin
declare @sql nvarchar(100)
Set @sql='Select * from '+@tableName
exec sp_executesql @sql
end
@tableName varchar(50)
as
begin
declare @sql nvarchar(100)
Set @sql='Select * from '+@tableName
exec sp_executesql @sql
end
Subscribe to:
Posts (Atom)