SQL select and alter table in same statement - sql

I have a table where I would like to select certain columns and then create transformed columns based on that selection. Due to security reasons, I'm not able to create a new table and thought there may be a way to SELECT and ALTER in the same statement.
My statement below runs, but the column is not produced. Am I doing something wrong/is this approach not possible? Is there a better approach?
SELECT * col1,col2,col3 FROM db
AS db2
ALTER TABLE db2 ADD col4 AS (transformation) PERSISTED
Guidance and recommendation is appreciated.

You likely need just perform a query or, if you want query data often, then view in database.
In SQL server and most likely in other SQL variants you can create view with next statement (db would be table name, db2 view name):
create view db2 as
select col1, col2, col3, (transformation) as col4
from db
Usually you need to grant some permissions to it, unless you are sole user of it.
Creating a view is one-time task, afterwards you can query data as follows:
select col1, col2, col3, col4
from db2
where ...

Related

why dbeaver puts ""(quotes) to my column name?

I created a table in my SQLite database in DBeaver. When I generate SELECT query:
SELECT "col1", col2, col3, col4 FROM mytable;
there is double quotes on my first column name. I directly created it as col1 and in table view it shows as col1. But in SQL query it "col1". There is something that i don't know related to SQLite? col1,3,4 are TEXT by the way.
I rewrite its name in table view but its still same.

Use SSIS To Copy A Table's Structure And Data With A Different Name

Using SSIS and the example tables myTable and Test_myTable I would like to:
Drop And Create Table the table Test_myTable.
Fill Test_MyTable with data from myTable, as it stands.
Is there a particular task that does this? I was looking into the property Expression Editor in the Transfer SQL Server Objects Task Editor and thought that putting an expression on the CopyAllObjects property would work but I'm not sure if that is the correct path.
I guess you can make use of Execute Sql Task for this and simply execute the following statements inside your task.
Instead of Drop and Create simply Truncate table, as dropping table means you will have to give permission to users if you have some sort of restrictions and only some specific user who can access the data.
Without Dropping the table
TRUNCATE TABLE Test_myTable;
GO
INSERT INTO Test_myTable (Col1, Col2, Col3, .....)
SELECT Col1, Col2, Col3, .....
FROM myTable
GO
Drop Table and Create
If for some reason you have to drop table and re-create it again you could execute the following statements inside your execute sql task.
--Drop tables if exists
IF OBJECT_ID('dbo.Test_myTable', 'U') IS NOT NULL
DROP TABLE dbo.Test_myTable
GO
--Create and populate table
SELECT Col1, Col2, Col3, .....
INTO dbo.Test_myTable
FROM myTable
GO

SSIS data from one table to another row by row by making it nto two rows with different computation in a SP

I need to move data from one table to another in the same database using an SSIS package, The data from source table is taken and it is computed to form two rows differently and load it into destination table.
I prefer using a stored procedure for this. Kindly help me out here..
A stored procedure to copy data from one table to another:
CREATE MyStoredProcedure AS
INSERT INTO Table1 (Col1, Col2, Col3)
SELECT Col1, Col2, Col3 FROM TABLE2
Now put this in an execute SQL Task inside your SSIS:
EXEC MyStoredProcedure
Now consider whether you need the overhead of a SSIS package to do this when you can just run it in a SQL step in a SQL Agent job.

How to manually move rows from one database to the next?

I have two databases on the same server. One db is newer than the other and has had its schema modified quite a bit. I want to transfer data from a table in the old db to a table in the new db but I need total control over the process so I can mold the old data to fit the new schema.
[NewDB].[dbo].[Aliases]
[OldDB].[Terminal].[Alias]
I'm not very adept at SQL yet. Is there a way I can loop through all the records in the old table and then on each iteration of the loop craft a custom insert statement for the new table?
Try an INSERT SELECT statement.
INSERT INTO [NewDB].[dbo].[Aliases]
SELECT columns
FROM [OldDB].[Terminal].[Alias]
http://msdn.microsoft.com/en-us/library/ms174335(v=sql.100).aspx - derived table section
http://msdn.microsoft.com/en-us/library/ms189499(v=sql.100).aspx
Do it the same way as you would if both tables were in the same database. Just fully qualify the names of the tables. Example:
INSERT INTO [NewDB].[dbo].[Aliases] (col1, col2, col3)
SELECT LEFT(col1,3), col2, col3 FROM [OldDB].[Terminal].[Alias]
Depending on the data size you wish to transfer, you might want to consider using BULK INSERT: http://msdn.microsoft.com/en-us/library/ms188365.aspx
INSERT INTO NEWDB..TABLENAME( *fieldlist* )
SELECT *fieldlist* FROM OLDDB..TABLENAME

SQL Server Generate Script To Fill Tables With Data From Other Database?

Let's say I have two databases with identical tables, but one database's tables contains data while the other doesn't. Is there a way in SQL Server to generate a script to fill the empty tables with data from the full tables?
If the tables are identical and don't use an IDENTITY column, it is quite easy.
You would do something like this:
INSERT INTO TableB
SELECT * FROM TableA
Again, only for identical table structures, otherwise you have to change the SELECT * to the correct columns and perform any conversions that are necessary.
And, to add to the #WilliamD answer, if there is an IDENTITY column you can use a variation of the INSERT statement.
Assuming you have two columns (Col1 and Col2, with Col1 having IDENTITY property) in the tables, you can do the following:
SET IDENTITY_INSERT TableB ON
INSERT INTO TableB (col1, col2)
SELECT col1, col2 FROM TableA
SET IDENTITY_INSERT TableB OFF
It's necessary to list the columns in this situation.