I've been asked to export data out of 100 tables.
for each table it would be something like this:
select * from table1
where datepart(yyyy,table1.DOS)=2011
and datepart(mm,table1.DOS)=01
and then:
select * from table1
where datepart(yyyy,table1.DOS)=2011
and datepart(mm,table1.DOS)=02
etc...
i would need to do this for every YEAR and for every MONTH for every table
i need to export these data sets to a CSV
can you please give me some guidance on how i can automate this instead of using the IMPORT/EXPORT wizard manually?
Dynamic SQL with sp_msforeachtable. Here is a link with info and examples:
http://weblogs.asp.net/nunogomes/archive/2008/08/19/sql-server-undocumented-stored-procedure-sp-msforeachtable.aspx
My Suggestion - Window Services help you out by setting the timing(Scheduling) in Application Configuration file
Related
Many times a day I have to write similar queries to get single record:
select t.*
from some_table t
where t.Id = 123456
maybe there is some shortcuts for retrieving single record? Like entering id, table and SQL server generates rest code automatically
In Sql Server Go to
Tools-> Options-> Environments->Keyboard
You will get shortcuts, there you can define your own as well as get the standards.
you can set a short cut for a fully executable query like
select * from table where id =20
but not like below
select * from
I have a simple sqlite database with several tables like this
schedule_20140824
schedule_20140825
schedule_20140826
...
How do I generate a select statement into a table for given a date?
For example, let's say today is the 26th and I wanted my app to access today's schedule table. What is the correct way to do the following?
SELECT * FROM 'schedule_'||strftime('%Y%m%d','now');
Any advice would be appreciated. Thanks!
SQLite itself cannot generate SQL dynamically.
You have to do this in your programming language:
db.execute("SELECT * FROM schedule_" + date)
I want to export some data from SQL Server to Oracle but I have this scenario:
My table in SQL I need to save my ID in a parameter in SSIS to use this because I want to run this package every 30 minutes.
So if my last ID in SQL exported to oracle is 10, I will save this on a variable in SSIS to change my query like this "SELECT * FROM TABLE WHERE ID > PARAMETER"
But I have no idea how to do it. help me
You can't store a variable in SSIS and have it last between sessions. The ID is in a table, yes? You can do something along the lines of
select
*
from
table
where
id > (select max(id) from <destination table>)
Or, if you're looking at completely different databases, you can populate a variable with the max(id) from your destination, and then plug that variable into your select from your source.
I'm going through all tables in a database trying to determine which tables are old (have not been altered in a long time). I've been going through and flagging all tables with old DTS's as "old"
Is there a more efficient way to do this? Can I run a statement that scans all tables in a database for date-timestamp fields and then looks at the most recent ones?
Thank you in advance for any help you can provide!
You can use the INFORMATION_SCHEMA.COLUMNS view to retrieve all the tables having the timestamp column (assuming the column name is known and not used for other columns).
Use these to generate Dynamic SQL of the form
SELECT COUNT(*) ,#TableName FROM #TableName WHERE #TimeStampColumn < #TimestampToCheck
The tables where count(*) is 0 are the ones you need to look at
I would do something like this:
Show a COUNT(*) of rows written in the last year... if you get 0, you know the table isn't in use.
Use the information_schema to get the columns/tables.
SELECT 'SELECT COUNT(*) AS ['+TABLE_NAME+'] FROM ['+TABLE_CATALOG+'].['+TABLE_SCHEMA+'].['+TABLE_NAME+'] WHERE '+COLUMN_NAME+' > DATEADD(YY,-1,CONVERT(DATETIME,FLOOR(CONVERT(FLOAT,GETDATE()))))'
-- SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE DATA_TYPE='DATETIME'
--AND (COLUMN_DEFAULT='(getdate())' OR COLUMN_DEFAULT='CURRENT_TIMESTAMP')
You can add the last line in, if you only want columns with a default value.
Then take the output, copy to a new window, and run it!
I am using sql server 2005
I have a table [say tblHistory] and this table contains 100 rows.
I have created the same table at the server, but the table doesn't have the data, I want data from tblHistory to convert into
INSERT INTO tblHistory ------
so that I could run the script on the server to fill the database.
To generate all the INSERT INTO statements you need based on table data, take a look at this project: http://www.codeproject.com/KB/database/sqlinsertupdategenerator.aspx
you need to create a linked server between the two servers and then you do something like this
INSERT INTO tblHistory
select * from LinkedServerNAme.DatabaseName.SchemaName.tblHistory
To add a linked server read this http://msdn.microsoft.com/en-us/library/ms190479.aspx
BTW you can also use OPENROWSET, OPENDATASOURCE, SSIS or bcp out and bcp in
Not sure I understand the question... you just want to copy the contents of one table into another?
INSERT INTO newTable SELECT * FROM tblHistory
If the new table doesn't already exist, you can use SELECT INTO:
SELECT *
INTO new_table
FROM tblHistory
But that's the caveat - it has to be a new table, no data already in there.
Otherwise, use:
INSERT INTO new_table
SELECT x.* --preferrable to actually define the column list than use *
FROM OLD_TABLE x
You should check out the SSMS Tools Pack - one of its feature is the ability to generate those INSERT scripts you're looking for!
Get the SSMS Tool Pack from this site - it's an add-in for SQL Server Management Studio - highly recommended!