How can I update all the tables which have Datetime fields in a database?
Eg. My DB has 50 Tables which has a column in the 50 tables which is of DATETIME Type.
I need to update the Column of datetime type with a new value.
How can I write a generic query to update datetime column in all the tables in the DB with a new value of datetime?
While I question the value of this in an unprecedented way (you should be proud!), you can't write a generic query that does this without dynamic SQL.
DECLARE
#sql NVARCHAR(MAX) = N'',
#newDateTime DATETIME = '20131217 07:30';
SELECT #sql += N'
UPDATE ' + QUOTENAME(s.name) + '.' + QUOTENAME(t.name)
+ ' SET ' + QUOTENAME(c.name) + ' = #newDateTime;'
FROM sys.columns AS c
INNER JOIN sys.tables AS t ON c.[object_id] = t.[object_id]
INNER JOIN sys.schemas AS s ON t.[schema_id] = s.[schema_id]
WHERE c.system_type_id = 61;
EXEC sp_executesql #sql, N'#newDateTime DATETIME', #newDateTime;
Related
Already checked this post: SSIS and sending query with date to Oracle
And I'm using variable query per below thread
SSIS - Using parameters in Oracle Query using Attunity Oracle Datasource
Tool used: VS-2019
Data flow: MS Oracle Source (for VS-2019)
My source is Snowflake cloud. I'm successfully able to get the max date from table and store in Object type variable (named:- #var_Snowflake_Table_maxDate). Then I use a script task to convert the value to string type.
Code for script task is:
public void Main()
{
OleDbDataAdapter A = new OleDbDataAdapter(); //using System.Data.OleDb; ADDED above in NAMESPACES
System.Data.DataTable dt = new System.Data.DataTable();
A.Fill(dt, Dts.Variables["User::var_Snowflake_Table_maxDate"].Value);
foreach (DataRow row in dt.Rows)
{
object[] array = row.ItemArray;
Dts.Variables["User::var_CreateDate"].Value = array[0].ToString();
}
Dts.TaskResult = (int)ScriptResults.Success;
}
This sets my param of #var_CreateDate String type correctly. I tried this on local machine and was able to pass the value to a native instance of sql-server(yes NOT oracle). Just to test my parameters from script task works.
Finally: I'm using VS-2019's MS Oracle Source to pass the value into Oracle cloud server. Sample query's I have tried
"select * from Table where rownum <= 5 and NVL(CREATE_DATE,UPDATE_DATE) = " +"'05-09-2020'"
::::evals to::::
select * from relate.awd_acct_activity where rownum <= 5 and NVL(CREATE_DATE,UPDATE_DATE) = '2020-05-09'
and this works. But value is hard coded.
Try 2:
"select * from table where rownum <= 50 and
NVL(CREATE_DATE,UPDATE_DATE) = " +"'#[User::var_CreateDate]'"
Try 3:
"select * from table where rownum <= 50 and
NVL(CREATE_DATE,UPDATE_DATE) = to_date(" +"'#[User::var_CreateDate]'"+")"
Try 4:
"select * from table where rownum <= 50 and
NVL(CREATE_DATE,UPDATE_DATE) = to_date(" +"'#[User::var_CreateDate]'"+",'YYYY-MM-DD')"
None of try 2 through 4 eval correctly. Can I have some guidance into how to pass this parameter to Oracle cloud.
Thanks.
I'm assuming you're trying to figure out the syntax for a variable, that would hold the query text. You can try something like this:
"select * from table where rownum <= 50 and
NVL(CREATE_DATE,UPDATE_DATE) = to_date(" + "'" + #[User::var_CreateDate] + "'" + ",'YYYY-MM-DD')"
I'm trying to edit this code to be dynamic as I'm going to schedule it to run.
Normally I would input the date in the where statement as 'YYYY-MM-DD' and so to make it dynamic I changed it to DATE(). I'm not erroring out, but I'm also not pulling data. I just need help with format and my google searching isn't helping.
PROC SQL;
CONNECT TO Hadoop (server=disregard this top part);
CREATE TABLE raw_daily_fcast AS SELECT * FROM connection to Hadoop(
SELECT DISTINCT
a.RUN_DATE,
a.SCHEDSHIPDATE,
a.SOURCE,
a.DEST ,
a.ITEM,
b.U_OPSTUDY,
a.QTY,
c.case_pack_qty
FROM CSO.RECSHIP a
LEFT JOIN CSO.UDT_ITEMPARAM b
ON a.ITEM = b.ITEM
LEFT JOIN SCM.DIM_PROD_PLN c
ON a.ITEM = c.PLN_NBR
WHERE a.RUN_DATE = DATE()
AND a.SOURCE IN ('88001', '88003', '88004', '88006', '88008', '88010', '88011', '88012',
'88017', '88018', '88024', '88035', '88040', '88041', '88042', '88047')
);
DISCONNECT FROM Hadoop;
QUIT;
When RUN_DATE is a string you can generate the current date string in-line on the SAS side
WHERE a.RUN_DATE = %str(%')%sysfunc(date(),yymmdd10.)%str(%')
AND ...
or
WHERE a.RUN_DATE = %sysfunc(quote(%sysfunc(date(),yymmdd10.),%str(%')))
AND ...
For the case of RUN_DATE being a string containing DATE9 formatted values, change the yymmdd10. to date9.
change:
WHERE a.RUN_DATE = DATE()
to:
WHERE a.RUN_DATE = PUT(date(), YYMMDD10.) AS date
I'm using jdbc to execute query statements (in jruby)
# made-up example
sql = "select " +
"c.type as cartype, " +
"o.id as ownerid, " +
"o.type as ownertype " +
"from cars c " +
"inner join owners o " +
"on c.vin = o.vin"
# 'stmt' gotten with jdbc-connection.create_statement()
result_set = stmt.execute_query(sql)
meta_data = result_set.get_meta_data()
col_count = result_set.get_column_count()
I can query the various column aliases (get_column_name) and tables (get_table_name) for each column through the column indexes, but I also need the actual/physical names of the columns, un-aliased.
How do I get the physical/actual name of column, as it is defined in the schema ("ownerid" column alias is column "id", for instance)?
From tests with other database types, it looks as though this is database+driver specific. Using mysql get_column_name returns the actual/physical column name while get_column_label returns the alias. As an aside, both database types (mysql and sqlite) return the physical table name through get_table_name.
Is there a way in SQL Server 2005 Express to generate a script that would create a VIEW based on tables primary/foreign key relationships?
I have multiple databases with thousands of tables, and its very time consuming to look at the "table dependencies" and try to JOIN data in the query window.
Thanks
This Query return Create statement for tables, but you must first note that:
1. only works for 1 column foreign key references
2. has not been tested for sql server express 2005, but works fine for Sql server 2005
create function dbo.func59C217D64BC54EA0B841BF1AB43D9398(#table1 nvarchar(1000), #table2 nvarchar(1000))
returns nvarchar(max)
as
begin
declare #sql nvarchar(max)
set #sql = ''
select #sql = #sql + dr + '.[' +cc +'] AS ['+ cc+ ISNULL(rr,'') + '],'
from (
select column_name cc,
dr = case when table_schema + '.' +table_name = #table1 then 'a' else 'b' end,
cast(NULLIF(row_number() over (partition by column_name order by table_name),1) as nvarchar) rr
from INFORMATION_SCHEMA.COLUMNS where table_schema + '.' +table_name in
(#table1,#table2)
) i
return substring(#sql,1,len(#sql) - 1)
end
GO
select cast(
'CREATE VIEW ['+r.CONSTRAINT_SCHEMA+'].[vw' +cp.TABLE_NAME+cf.TABLE_NAME+ ']
AS SELECT '+dbo.func59C217D64BC54EA0B841BF1AB43D9398(cp.TABLE_SCHEMA+'.' +cp.TABLE_NAME,cf.TABLE_SCHEMA+ '.' +cf.TABLE_NAME)+' FROM ['+cp.TABLE_SCHEMA+'].['+cp.TABLE_NAME+'] AS a
JOIN ['+cf.TABLE_SCHEMA+'].['+cf.TABLE_NAME +'] AS b
ON a.['+cp.COLUMN_NAME+'] = b.['+cf.COLUMN_NAME +']' as ntext) as sql
from INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS r
JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE cf ON r.CONSTRAINT_NAME = cf.CONSTRAINT_NAME
JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE cp ON r.UNIQUE_CONSTRAINT_NAME = cP.CONSTRAINT_NAME
GO
DROP function dbo.func59C217D64BC54EA0B841BF1AB43D9398
hope I helped, or at least make you started
I have a domain class that when persisted to Oracle must update a column to sysdate. NHibernate must only generate 1 SQL. e.g.
update person set age = 12, stamp = sysdate where id = 1;
Can this be done?
EDITED:
Could be something like:
Person person = (Person)session.Get(typeof(Person), 1);
session.SetFunction(person, "stamp", Functions.CurrentTimestamp);
person.Age = 12;
session.Flush();
You could update the stamp via a database trigger:
create trigger person_trg
before update
for each row
begin
:new.stamp := sysdate;
end;
Then all hibernate would need to do is "persist" the change to age (for example).
If you want to do that on demand, you can just execute the query above:
session.CreateQuery("update person set age = 12, stamp = sysdate where id = 1")
.ExecuteUpdate();
Interestingly, that is both valid HQL and SQL.