I'm a bit confused on the update statement but here's what I have: I have these two employees and their respective alpha numeric codes.
select * from cm.bo.hotlist('08Z')
where State = 'ca'
select * from cm.bo.hotlist('06D')
where State = 'ca'
The table has certain cities associated with each employee, the top select statement has these list of cities associated with '08Z'... let's say.
New York
Chicago
I would like to move those cities to the employee '06D'
How would I got about updating?
The confusing part for me is the table is a table-valued function.
Any help would be greatly appreciated. Thank you.
maybe something like so:
update CITY cm.bo.hotlist('06D')
where CITY in (New York, Chicago)
So what you want is:
Update cm.bo.hotlist('08Z')
set
<EmployeeID Column> = '06D'
where
city in ('New York', 'Chicago')
For everyone who comes here, yes, an in-line table value function is updateable as long as the underlying data set is updateable. A code sample:
IF EXISTS(select * from sys.objects where name = 'test' and schema_id = schema_id('dbo')) BEGIN DROP TABLE dbo.test; END
CREATE TABLE dbo.test(Employee varchar(10), city varchar(10));
CREATE FUNCTION [dbo].[getEmployeeCities] ( #employee varchar(10) RETURNS TABLE AS
RETURN ( SELECT * from test where employee = #employee );
insert into dbo.test select 'A', 'Chicago';
insert into dbo.test select 'B', 'New York';
select * from dbo.test;
update dbo.getEmployeeCities('A')
set Employee = 'B'
where city = 'Chicago';
select * from dbo.test;
Update Tablename
SET employes ='06D'
where CITY IN ('NEW York', 'Chicago')
Related
I'm migrating data from one table to another table in SQL Server, In this process what I need to do is "I have 10 columns in old table one column is 'CityName' which is varchar and in the new table, I have a column 'CityId' which is an integer. And I have other table which has data about city id and names. I need store the appropriate cityId in new table instead of CityName. Please help me. Thanks in advance.
You'll need to join the source table to the CityName field in the city information table:
INSERT INTO dbo.Destination (CityID, OtherStuff)
SELECT t1.CityID, t2.OtherStuff
FROM CityInformationTable t1
INNER JOIN SourceTable t2
ON t1.CityName = t2.CityName
Below should give you an idea, you need to inner join to your look up table to achieve this.
declare #t_cities table (Id int, City nvarchar(20))
insert into #t_cities
(Id, City)
values
(1, 'London'),
(2, 'Dublin'),
(3, 'Paris'),
(4, 'Berlin')
declare #t table (City nvarchar(20), SomeColumn nvarchar(10))
insert into #t
values
('London', 'AaaLon'),
('Paris', 'BeePar'),
('Berlin', 'CeeBer'),
('London', 'DeeLon'),
('Dublin', 'EeeDub')
declare #finalTable table (Id int, SomeColumn nvarchar(10))
insert into #finalTable
select c.Id, t.SomeColumn
from #t t
join #t_cities c on c.City = t.City
select * from #finalTable
Output:
Id SomeColumn
1 AaaLon
3 BeePar
4 CeeBer
1 DeeLon
2 EeeDub
may anyone please share your expertise .
i have a table contain customerid and it has only 1 specific value at a given time.
DDL Table:
CREATE TABLE CUSTOMER
(
[CUSTOMERID] [int] NOT NULL
)
INSERT CUSTOMER
SELECT 100 UNION
SELECT 105 UNION
SELECT 108
here in example i have showed 3 value, but in table there will be always 1 value at given time.
and before loading other value, first value will get deleted.
now i have 60 views created for 60 different customer. each customer has different data pattern and logic associated with it.
example of views. Note: This is just example and not actual logic
CREATE VIEW V1
as
(
select * from dwhtable
where colummnx = '100'
)
similarly
CREATE VIEW V2
as
(
select * from dwhtable
where columnx = '105
)
Mapping Table
create table mapping_table
(
custID int,
view_name varchar (150)
)
insert mapping_table
select 100, 'v1' union all
select 105, 'v2' union all
select 108, 'v3'
i am trying to automate the entire process either by SP or Function or DynamicSQL.
So my views will run for a specific customer.
Now How do we know which view goes with which customer.
The customer table will come in picture, and we need to match the value in customer table and which view contain that value.
i am not sure whether it is achievable in sql.
Please share your expertise.
Thanks
Not sure if I understand the task correctly. So the requirement is to have one view per customer. We need to get all the columns from that view for a given customer.
So you need to have a mapping table Customer_View_Mapping (customer_id int, view_name varchar(128))
Then you can get the name of view:
declare #name varchar(128)
select #name = view_name from Customer_View_Mapping where customer_id = #customer_id
the you can execute something like this:
EXEC (N'select * from ' + #name)
You can insert the results to the temp table if required.
INSERT INTO #table (<column list>)
EXEC (N'select * from ' + #name)
I can use OUTPUT to generate the before and after row values when doing an update to a table. And I can put these values into a table.
But they go into the table in 1 single row.
I need the before values as a row, and the after values as a new row.
So this snippet isn't working for me at the moment, but it shows the thing I am trying to do. Is there any way to do this using OUTPUT.
update dbo.table1
set employee = employee + '_updated'
OUTPUT 'd',DELETED.* INTO dbo.table2,
OUTPUT 'i',INSERTED.* INTO dbo.table2,
WHERE id = 4 OR id = 2;
This snippet below works, but only creates a single row:
update dbo.table1
set employee = employee + '_updated'
OUTPUT 'd', DELETED.*, 'i', INSERTED.* INTO dbo.table2,
WHERE id = 4 OR id = 2;
I can do it using triggers, but that's not allowed in this case.
And I can do it manually (selecting what I'm going to update into table2, then doing the update...)
Any tips or hints appreciated on how to do it just using just OUTPUT in the update?
Rgds, Dave
To elaborate on an answer given in the comments:
declare #TempTable table (
d_id int,
d_employee varchar(50),
d_other varchar(50),
u_id int,
u_employee varchar(50),
u_other varchar(50)
)
update Table1
set employee = employee + '_updated'
output deleted.id d_id, deleted.employee d_employee, deleted.other d_other,
inserted.id u_id, inserted.employee u_employee, inserted.other u_other
into #TempTable
where id = 4 or id = 2;
insert Table2 (change_type, employee_id, employee, other)
select
'd',
d_id,
d_employee,
d_other
from #TempTable
union all
select
'i',
u_id,
u_employee,
u_other
from #TempTable
I made some assumptions about your schema as it wasn't given, but this should get you started.
I have created a view in SQL server 2008 which consists of the joining of about 5 different tables to access the data i need.
How would i create an update trigger to update a field within this view? So for example if i wanted to update all first names to peter if the last name is smith.
If Fname = peter
update Sname to Smith
end if
Many thanks in advance
UPDATE
this is what i have so far
CREATE TRIGGER SurName
ON ViewCustomer
AFTER UPDATE
AS
if FName= 'Peter'
BEGIN
update ViewCustomer set SName= 'Smith'
SET NOCOUNT ON;
END
GO
This should do the job.
Table and view definitions.
CREATE TABLE Customer
(ID int,
FName varchar(200),
SName varchar(200),
RoleID int);
CREATE TABLE CustomerRole
(RID int,
Name varchar(100));
CREATE VIEW ViewCustomer AS
SELECT *
FROM Customer JOIN CustomerRole on RoleID = RID;
Trigger definition.
CREATE TRIGGER ViewCustomerTrigger ON ViewCustomer
INSTEAD OF UPDATE
AS
BEGIN
SET NOCOUNT ON
UPDATE Customer
SET FName = I.FName,
SName = CASE I.FName WHEN 'Peter' THEN 'Smith' ELSE I.SName END,
RoleID = I.RoleID
FROM INSERTED I JOIN Customer C ON I.ID = C.ID
UPDATE CustomerRole
SET Name = I.Name
FROM INSERTED I JOIN CustomerRole R ON I.RID = R.RID
END
GO
Sample data
INSERT INTO Customer (ID, FName, SName, RoleID)
VALUES (1, 'John', 'Wayne', 1);
INSERT INTO Customer (ID, FName, SName, RoleID)
VALUES (2, 'Jack', 'Jackson', 1);
INSERT INTO CustomerRole (RID, Name)
VALUES (1, 'Manager');
This update will cause the trigger to update the SName to 'Smith'
UPDATE ViewCustomer
SET FName = 'Peter'
WHERE ID = 1
Here is a SQLFiddle for it.
You need to create an INSTEAD OF trigger for views. You can check on:
http://msdn.microsoft.com/en-us/library/ms188601.aspx
I am having a table Student and i have a set of 20 names.
by using his sql
select name from student st where st.name in (
'abc', 'xyz', . . .
)
i can find out all student names which are in table and in the set.
Now, how can i find out which out of these 20 names are not in Student table.
I'm assuming you want the names themselves.
One option is to create a table with all the available student names, then select from it rows which don't have corresponding rows in the student tables, it will look something like this
select name from student_names
where name not in (select name from students)
CREATE TABLE student(name VARCHAR(255));
INSERT INTO student VALUES('a'), ('abc');
CREATE TABLE temp(x VARCHAR(255));
INSERT INTO temp VALUES('abc'), ('xyz');
SELECT x FROM temp WHERE
NOT EXISTS (SELECT * FROM student st WHERE st.name = x);
Depending on the database you use, there might be an easier way. There is also a way using UNION.
SELECT NOT IN ?
postgresql: http://archives.postgresql.org/pgsql-sql/2002-08/msg00322.php
DECLARE #names table ( name varchar(100) )
INSERT INTO #names VALUES ('abc')
...
INSERT INTO #names VALUES ('xyz')
SELECT name FROM #names WHERE name NOT IN ( SELECT DISTINCT Name FROM Student )
select name from student where name not in (
select name from student st where st.name in (
'abc', 'xyz', . . .
))
EDIT: I might not get what you are looking for. Please run following script and it is giving the results.
declare #student table
(
name varchar(50)
)
insert into #student select 'james'
insert into #student select 'will'
insert into #student select 'bill'
insert into #student select 'adam'
insert into #student select 'jon'
insert into #student select 'white'
insert into #student select 'green'
select name from #student where name in ('james', 'will', 'bill')
select name from #student where name not in (select name from #student where name in ('james', 'will', 'bill'))
Assuming that the tool you are using can generate dynamic sql, try generating an inline view consisting of your set of user names - like so:
select 'abc' check_name union all
select 'xyz' check_name union all
...
(The syntax of the inline view may depend on which version of SQL you are using - some versions of SQL require a from [dummy_table] clause in select statements that are not accessing a table.)
Then construct a query using this inline view with a not exists in student clause, like this:
select check_name from (
select 'abc' check_name union all
select 'xyz' check_name union all
...
) ilv where not exists
(select null from student st where st.name = ilv.check_name)