Transact SQL :: How To Automate Names Of Select Into Tables
Jun 3, 2015
I have a number of tables, that I need to copy with data intact, about once a year.
Let's say the table name is 'dbo.MyTable'
I just do a SQL Statement like this:
SELECT * INTO dbo.MyTable'BU06032015 FROM dbo.MyTable
How can I automate this so that it does the Select Into statement and automatically adds the 'BU' + the current date?
View 5 Replies
ADVERTISEMENT
Mar 14, 2007
Hi,I'm trying to create views on all my existing tables and for that I'dlike to create a script or so.I don't want to specify the '*' for the columns in the create viewstatement. I prefer to specify the column names.I have the column names int sys.columns table but Do not know how tohandle them to have a statement like that:CREATE VIEW myVIEWWITH SCHEMABINDINGASSELECT col1name, col2name, col3name, etc...from sys.columns....?????.....Anyone can help?thx,Chris
View 1 Replies
View Related
Nov 11, 2015
I am new in the automation, how I can automate excel with SQL server.?i need to update with reports will be automatically send to my client. Data is kept in the database.
View 4 Replies
View Related
Jul 13, 2015
I have the following two tables....
tblTimeEntry
-entryID
-entryDate
-entryUser
-entryJob
-entryTask
-entryWeekNo
tblWagesWeeks
-weekID
-weekDay
-date
I want to select all of the date and weekDay values from tblWagesWeeks for a specific weekID. I also want to show all entries fromtblTimeEntry for the weekID when a record exists. If data does not exist in fromtblTimeEntry I want to display a blank entry but still need weekDay and date from tblWagesWeeks.
View 11 Replies
View Related
Jun 18, 2015
i have 3 tables names parent, child1, child2 parent has 1 record, child1 has 2 record and child 3 has 3 records the script
select Parent.*,child1.f1,child2.f2 from child1 inner join Parent on parent.id =child1.id
inner join child2 on child1.id =child2.id
running above query gives me sixes rows but i want only all rows of childs but not their Cartesian products
Object: Table [dbo].[Parent] Script Date: 06/18/2015 17:33:02 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Parent](
[id] [int] NOT NULL,
[code]....
View 8 Replies
View Related
May 2, 2007
I'm using SQL 2000, which is integrated with a VB.NET 2003 app.I have an Audit trigger that logs changes to tables. I want to applythis trigger to many different tables. It's the same trigger, with theexception of the table name. I could manually change the table name inthe trigger and create it, over and over, but I'd like to automatethis - by iterating through the collection of tables and passing thetablename to something that would create the audit trigger for thattable.Any ideas? With any scripting language I can create the text of theCREATE TRIGGER procedure for each table and use simple textreplacement, but how do I automate that inside of SQL?Thanks.
View 9 Replies
View Related
Nov 3, 2015
Here below is the perfect query i made which is working fine and giving me the sql output but just only need is how to convert to excel and automate the job scheduling so that it run on everyday and send the mail with attachment .
SELECT DN, cn, displayName, mail, objectClass, sAMAccountName, Company, givenName, sn
FROM
(
SELECT DN, cn, displayName, mail, objectClass, sAMAccountName, Company, givenName, sn, 1 [ordering] FROM alpha.dbo.DCADFeed
where sAMAccountName collate SQL_Latin1_General_CP1_CI_AS in
[Code] ....
View 4 Replies
View Related
Oct 6, 2015
We are using SQL Server 2014 and SSDT-BI 2013. We have a reporting environment where business users create objects which need to be persisted for fiscal year reporting. Let's say for instance SQLSERVER1SRVR1 they create table objects like below in the reporting environment.
Accounting2014, Accounting2015 in AccountingDB;
Sales2014, Sales2015 in SalesDB;
Products2014, Products2015 in ProductsDB;
Inventory2014, Inventory2015 in InventoryDB etc....
These tables are persisted for auditing in a different environment SQLSERVER2SRVR2 for finance & audit folks.We would want to automate this process using SSIS to create tables in corresponding database and load data. I tried using For Each Loop container but the catch is I could loop the source or destination but how do we loop on Source & Destination at the same time (i.e when source is in AccountingDB destination to be AccountingDB, source SalesDB then destination SalesDB so on etc....
View 6 Replies
View Related
Oct 7, 2015
I am using the following select statement to get the row count from SQL linked server table.
SELECT Count(*) FROM OPENQUERY (CMSPROD, 'Select * From MHDLIB.MHSERV0P')
MHDLIB is the library name in IBM DB2 database. The above query gives me only the row count of table MHSERV0P. However, I need to get the names, rowcounts, and sizes of all tables that exist in MHDLIB librray. Is it possible at all?
View 1 Replies
View Related
Oct 5, 2007
Firstly I consider myself quite an experienced SQL Server user, andamnow using SQL Server 2005 Express for the main backend of mysoftware.My problem is thus: The boss needs to run reports; I have designedthese reports as SQL procedures, to be executed through an ASPapplication. Basic, and even medium sized (10,000+ records) reportingrun at an acceptable speed, but for anything larger, IIS timeouts andquery timeouts often cause problems.I subsequently came up with the idea that I could reduce processingtimes by up to two-thirds by writing information from eachcalculationstage to a number of tables as the reporting procedure runs..ie. stage 1, write to table xxx1,stage 2 reads table xxx1 and writes to table xxx2,stage 3 reads table xxx2 and writes to table xxx3,etc, etc, etcprocedure read final table, and outputs information.This works wonderfully, EXCEPT that two people can't run the samereport at the same time, because as one procedure creates and writesto table xxx2, the other procedure tries to drop the table, or read atable that has already been dropped....Does anyone have any suggestions about how to get around thisproblem?I have thought about generating the table names dynamically using'sp_execute', but the statement I need to run is far too long(apparently there is a maximum length you can pass to it), and evenbreaking it down into sub-procedures is soooooooooooooooo timeconsuming and inefficient having to format statements as strings(replacing quotes and so on)How can I use multiple tables, or indeed process HUGE procedures,withdynamic table names, or temporary tables?All answers/suggestions/questions gratefully received.Thanks
View 2 Replies
View Related
Oct 30, 2015
I'm struggeling a bit with this one, goal is to show the first 3 characters of the name and replace the remaining characters with *** stars. Also the number of stars needs to match the lenght of the name as in the example above.I've tried:
REPLACE([ColName],
substring([Colname],
4,
LEN([colname]
)),
'*')
View 5 Replies
View Related
Jul 6, 2015
I am trying to find the Table and column names from the below.
Is there a way i can get table name and column name from query_plan column?
SELECT TOP 100 text, query_plan,cp.plan_handle
FROM sys.dm_exec_cached_plans cp
CROSS APPLY sys.dm_exec_sql_text(cp.plan_handle)
CROSS APPLY sys.dm_exec_query_plan(cp.plan_handle)
WHERE objtype='Adhoc'
View 5 Replies
View Related
Jul 21, 2015
SELECT TOP 100 text, query_plan, cp.plan_handle, qs.last_execution_time
FROM sys.dm_exec_cached_plans cp
CROSS APPLY sys.dm_exec_sql_text(cp.plan_handle)
CROSS APPLY sys.dm_exec_query_plan(cp.plan_handle)
JOIN sys.dm_exec_query_stats qs ON cp.plan_handle = qs.plan_handle
WHERE objtype='Adhoc'
I have below output:
Text Query_plan Plan_handle Lst_execution_time
Select id,name from person
<Showplan... dshkkgdaHqrqe13232423 2015-07-21 10:50:22.713
Update customer set Cid=3 where name='abc'
<Showplan... poasfvrqe13232423 2015-07-21 10:16:22.500
delete from orders where ORid=8
<Showplan... 2ase2423 2015-07-21 10:10:22.710
Select 1,2,3,4,5 from num
<showplan afqfqfq 2015-07-21 10:10:22.710
I am looking for
Text Query_plan Plan_handle Last_execution_time TabName colname
Select id,name from person <Showplan... dshkkgdaHqrqe13232423 2015-07-21 10:50:22.713
Person Id, name Update customer set Cid=3 where name='abc'
<Showplan poasfvrqe13232423 2015-07-21 10:16:22.500 customer Cid, name
delete from orders where ORid=8 <Showplan... 2ase2423 2015-07-21 06:10:22.710 Orders ORid
Select 1,2,3,4,5 from num <showplan afqfqfq 2015-07-21 10:10:22.710 nUM 1,2,3,4,5
View 2 Replies
View Related
Aug 4, 2015
Is there a way to bulk remove spaces from column names from all tables in a db?
View 6 Replies
View Related
May 12, 2015
I am able to get a list of columns in a table from the query I have written shown below:
select sc.name ColumnNames,st.name TableName from sys.columns sc inner join sys.tables st on sc.object_id=st.object_id
order by st.name
But I am looking for the resultset with the format below:
TableName Columns
employee employeeid,employeename,employeesalary
order orderid,address,price
View 2 Replies
View Related
Aug 14, 2015
How can I bulk remove spaces within table names in the whole db?
View 3 Replies
View Related
Jul 14, 2015
I have a table as below and need getting the desired result as below
Col1 Col2 Col3
A B
C
---desired result
t1 t2
Col1 A
Col2 B
Col3 C
[URL]
View 3 Replies
View Related
Sep 16, 2015
I don't know why this is so difficult. What I want to do is take a table name as a parameter to build a query and get an integer value from the result of the query. But from all of the research I have been doing, Dynamic SQL is bad in SQL server because of SQL Injections. But my users are not going to be supplying the table names.
Things I have learned:
- SQL Functions cannot use Exec to execute query strings.
- SQL Functions can return a concatenated string that could be used by a stored procedure to Exec the query string.
So how can I write a stored procedure that will
1. take a parameter
2. Pass the parameter to a function that will return a string
3. Execute that string as SQL
4. Get a return value from that SQL statement
5. Then finally, from a View, how can I pass a parameter to the stored procedure and get the returned value from the stored procedure to be used as a field in the View?
Numbers 3, 4, and 5 are where I am really stuck. I guess I don't know the proper syntax and limitations of SQL Server.
View 14 Replies
View Related
May 7, 2015
While looking forward to design a multi-columnar cross-tab query I am anxious to know if there could be a way to change the default names of the pivot columns? In other words for the query like the following can there be a way to apply anAS type command to reflect some other names, instead of having the four dates in heading? Something like Month_A, Month_B?
SELECT * FROM
(SELECT
X.REP_DT,
X.CUST_ID
AMOUNT_1
FROM
X) P
PIVOT (SUM(AMOUNT_1) FOR REP_DT IN ([2014-12-31], [2015-01-31], [2015-02-28], [2015-03-31])) PVT_01
View 3 Replies
View Related
Jul 23, 2013
I want to get all databases with it's owner Name ,I think I need to join following script result with sys.databases or sys.sysdatabases.I want a result set like this:
DatabaseName db_Owner
master sa
pubs valia
pubs eli
pubs maryam
AdentureWork eli
this script get all owners of the database that is in use:
Use DatabaseName
GO
SELECT members.name MemberName, roles.name RoleName,roles.type_desc,members.type_desc
View 10 Replies
View Related
May 26, 2007
I have a table that I am basically reduplicating a couple of times for each part of this database that I want to create.Each table basically has the same data: The tables will be called motherTable, fatherTable, sonTable, daughterTable and so on.I am pretty much using the following in each column: UserID, MotherID(or FatherID or SonID, etc., etc. and so on for each unique table), FirstName, LastName, MiddleName, BirthPlace, Photo, Age.I don't see an option to copy a table and just modify the second ID part and rename that table accordingly.How can I make this an easier way of creating these similar tables without retyping all these columns over and over again?Thanks in advance.
View 4 Replies
View Related
May 29, 2015
I have a SQL text column from SP_who2 in table #SqlStatement:
like 1row shown below :
"update Panel set PanelValue=7286 where PanelFirmwareID=4 and PanelSettingID=9004000"
I want to find what table and column names are in the text ..
I tried like below ..
Select B.Statement from #sp_who2 A
LEFT JOIN #SqlStatement B ON A.spid = B.spid
where B.Statement IN (
SELECT T.name, C.name FROM sys.tables T
JOIN sys.columns C
ON T.object_id=C.object_id
WHERE T.type='U'
)
Something like this : find the column names and tables name
View 18 Replies
View Related
Oct 30, 2015
I got a table which gets populated by stor proc where we pivot the Sum(Balance of mortgage) by YYYYMM for the whole duration of the loan term.
I have a requirement to rename the column header where the previous month end balance period be renamed to P0.
if we run the report today, then the balance as at 31/09 should show under column P0 which now shows under 201509 and then P0 keeps shifting with each month run.
How do I dynamically rename the column headers.
View 7 Replies
View Related
May 19, 2015
I want to create a raw SQL resultset for outputting to Excel with some artificial headers transposed over the top of the 2nd part of the Union's column names. The first part of the Union will be the Headers. Like this, the space to the left of the topmost columns is preferably empty ....
COL 1 COL2 COL3 COL4 etc. BEH BIG BPL etc.
************************************* INTAKT DEFEKT INTAKT DEFEKT INTAKT
************************************* B E B E B
I just want the text above as a 3 line header and there won't be any values obviously. Then the 2nd query will be joined to the above with a Union all. The 2nd query has all the same column names as what will be given in the first set above. What is the SQL Syntax for doing so? Do I have to use a from clause?
View 4 Replies
View Related
Oct 8, 2004
Hi All,
I need to get the names of tables and fields (or atleast the fields) from a database. Is there a query I can use that will return what all the db's tables and those tables fields are?
Thanks
Lee
View 3 Replies
View Related
Mar 17, 1999
------------
D.P. at 3/15/99 2:04:04 PM
Using VB5|6/ADO/MSSQL6.5. I want to get the names of tables in a query. I.e. if query is "Select Name, Title from Employee inner join job on employee.empid = job.empid" I want a way to get "Name, Title"
In VB 3 we used the VBSQL.VBX which had 2 functions for this: SQLTabCount and SQLTabName.
Thanks in advance
DP
------------
Gregory at 3/15/99 2:25:31 PM
dp ++++++++++++++++++
see my responses below
dp ++++++++++++++++++
if u need to get "Name" and "Title" - u're trying to get column(field) names??
-or-
if u need to get table names involved in a query - u need to get "Employee" and "Job"
so what exactly do u need??
********
dp ++++++++++++++++++
My bad... I wanted Employee, Job. The Table names.
dp ++++++++++++++++++
if u need to see column names - u can use Fields collection to get all fields in a recordset
I don't know if there's a way to get table names of tables involved in a query, in ADO.in RDO there's a property of a rdoColumn object to get table name (which doesn't work always anyway).
********
dp ++++++++++++++++++
actually this can be done in ADO. You can get the table from which the column was derived by using rs.fields(##).properties("BASETABLENAME") but I don't want to rely on this because I may not have a field from each table... What I really want is creat a recorset and ask it what tables it's drawing from. Again, VBSQL had SQLTabCount which gave you the number of tables in the query and then SQLTabName would give you the names by table number
dp ++++++++++++++++++
or, in select statement u can aliases:
select Name as 'Employee_Name',
title as 'Job_Title'
from employee, job
where employee.job_id = job.job_id
so, when you get all fields from fields collection, u can check field name
dp ++++++++++++++++++
yeah... i'll do this if I have to... looking for a better way (hopefully) first
dp ++++++++++++++++++
********
or , u can use ODBC API functions to get that information (for example using SQLColumns() function) - but, u'll need to connect first, to get connection handle, then prepare or execute statement to get statement handle, and then get information about columns (and table names).
dp ++++++++++++++++++
this sounds interesting.... where might I find more info about this??? If i create an ado recorset, will I be able to "analyze" it in this way via ODBC?
dp ++++++++++++++++++
View 1 Replies
View Related
Mar 7, 2005
Is there a way that you could get the column names for each table in a database using 1 query?
something like:
tbl colname
t1 catID
t1 catName
t2 prodID
t2 prodDesc
t3 cartID
...
...
I know it would be long, but I would just be searching through the saved output for specific names.
View 1 Replies
View Related
May 27, 2008
here a quick rundown of what i want to do:
database
database1
database hold all my good information.
database 2 is the copy for breaking.
Im too a point that i need to get my 2nd DB to by like the first.
I want to truncate all the tables in database2, and then export, or select from insert into.
I just need the data, not triggers or anything like that.
So:
How can i find the name of my tables (not views) in that database
For each (select table_name from system where database="database") copy database.table_name database1.table_name;
i hope that makes sense.
I just need to restore my devel DB back to the original so i can mess it up again,
Thanks
View 3 Replies
View Related
Sep 9, 1999
I have a situation in which i have to get the last value stored in the Primary Key for all the tables. Based on this value i have to update another table which stores the Table names and the last Key value for the table. The values in this table are not correct therefore i have to update it now. I was trying to write a cursor for this but the only problem is i can't get to know how to get the column name on which the primary key is defined for all the tables, thru code.
I would appreciate if someone could help me out with this.
View 4 Replies
View Related
Dec 16, 2005
I am attempting to how to truncate list of tables using STP. That is decalre a cursor for a list table names and then to truncate the table names one by one.
The code below shows what I want to achieve. I want to truncate all the tables with names beginning with ZZ but this is failing. I have tried using both delete and truncate.
Is it possible and what do I need to do?
-- Code
SET QUOTED_IDENTIFIER Off
GO
SET ANSI_NULLS ON
GO
create PROCEDURE dbo.Empty_ZZ
AS
DECLARE @tablename sysname
DECLARE @localname varchar(50)
DECLARE ZZtablenames_cursor CURSOR FOR
select [name]
from sysobjects
WHERE [xtype] = 'U'
and name like 'ZZ %'
OPEN ZZtablenames_cursor
FETCH NEXT FROM ZZtablenames_cursor
into @tablename
WHILE @@FETCH_STATUS = 0
BEGIN
set @localname = '[' + @tablename + ']'
TRUNCATE + @localname
FETCH NEXT FROM ZZtablenames_cursor
END
CLOSE ZZtablenames_cursor
DEALLOCATE ZZtablenames_cursor
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
View 4 Replies
View Related
Dec 13, 2013
If you need to inner join 2 tables that have some columns names that are the same, how can you have those columns be named differently in the query result without aliasing them individually?
Tried select a.*,b.* from tbldm a,tblap b where a.id=b.id hoping the col names in the result would have the a.s and b.s in front of them but they didn't.
View 2 Replies
View Related
Jan 26, 2007
We have recently copied a database from one machine to another. On the old machine, when we access the tables we do not need to use the username.tablename convention to query them. On the new box we do.
For example, to query a table called Page we would nee to do this on the new box.
SELECT *
FROM webdev.page
unfortunately all the code is written, without the username prefix. Is there a way to not use the username prefix?
Thank you for your help...sorry for the newbie type question.
View 3 Replies
View Related
Feb 28, 2008
Hi all,
From the INFORMATION_SCHEMA.TABLES view I want to return the TABLE_NAME of tables that have columns say, named Email and EmailStatusId. Is it possible to do this with a single select statement or would I have to use two selects for this?
Please advise.
Thanks in advance.
View 7 Replies
View Related