I am migrating all my VM Sql Server 2012 database to Azure Sql Database. In my current structure I am using cross database queries to fetch data from different database tables.
I have created external table to my parent table using below query
CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'yourPassword';
CREATE DATABASE SCOPED CREDENTIAL yourServeradminlogin
WITH IDENTITY = 'yourServeradminlogin',
SECRET = 'yourPassword';
CREATE EXTERNAL DATA SOURCE RefmyDemoDB2
WITH
(
TYPE=RDBMS,
LOCATION='testdbdemoserver.database.windows.net',
DATABASE_NAME='myDemoDB2',
CREDENTIAL= yourServeradminlogin
);
CREATE EXTERNAL TABLE [dbo].[Department](
[DeptId] [int] NOT NULL,
[Name] [varchar](50) NULL
)
WITH
(
DATA_SOURCE = RefmyDemoDB2
);
/****** Script for SelectTopNRows command from SSMS ******/
SELECT *
FROM [dbo].[Employee] E
INNER JOIN [dbo].[Department] D
ON E.DeptId = D.DeptId
I referred this link https://www.c-sharpcorner.com/article/cross-database-queries-in-azure-sql/
But when I create external table it doesn't shows table in external table folder like shown in below image.
In my case it directly showing in Tables folder.
Anyone knows why I don't see Department table in External Tables folder? How can I add such tables in External Tables folder?
External tables are available in Azure SQL only to support a feature called "Elastic Queries", that may solve your problem:
https://learn.microsoft.com/en-us/azure/sql-database/sql-database-elastic-query-overview
If this is not enough for you, and you really need full cross-database query support, you have to use an Azure SQL Managed Instance:
https://learn.microsoft.com/en-us/azure/sql-database/sql-database-managed-instance
which seems to be exactly what you need.
Related
In Azure Synapse, how can I check how a table is distributed. For example whether it is distributed in a round robin manner or with hash keys.
You can use the Dynamic Management View (DMV) sys.pdw_table_distribution_properties in a dedicated SQL pool to determine if a table is distributed via round robin, hash or replicated, eg
SELECT
OBJECT_SCHEMA_NAME( object_id ) schemaName,
OBJECT_NAME( object_id ) tableName,
*
FROM sys.pdw_table_distribution_properties;
It's the distribution_policy_desc column. Some sample results:
Don't confuse distribution and partitioning. I've updated the question.
pdw_table_distribution_properties is certainly a possibility as mentioned.
Or just generate the create DDL for that table using any Client (Data Studio, SSMS, VS Code with plugin, ...).
E.g. in Azure Data Studio, right click on the table and click "Script as Create".
Look for DISTRIBUTION in WITH clause.
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[sa_logs]
(
[version_number] [float] NULL,
[request_start_time] [datetimeoffset](7) NULL,
...
[referrer_header] [varchar](256) NULL,
)
WITH
(
DISTRIBUTION = ROUND_ROBIN,
HEAP
)
GO
or for a table with HASH:
...
DISTRIBUTION = HASH ( [hash_column] ),
...
I want to do horizontal partitioning in my SQL Server. Some of the tutorials say I need to click the table -> Storage -> Click Partition. something like below.
Issue: though, for my case, I am only seeing "Manage compression". I am not seeing "Create partition", In addition, I am not able to see "FileGroups" at the database level.
I am using Azure SQL
No Partition option showing up:
No File group option visible:
SSMS has that wizard blocked, as Azure SQL Database doesn't support using non-primary filegroups for partition schemes. You can provide feedback for SSMS here.
The DDL for partitioning in Azure SQL Database is the same as for regular SQL Server, so you can write it by hand, or use the SSMS wizard against a local SQL Server to generate the script. Just remember to point all partitions to the primary filegroup.
Minimally on Azure SQL Database, this looks like:
CREATE PARTITION FUNCTION pf(INT)
AS RANGE RIGHT FOR VALUES (1, 100, 1000);
CREATE PARTITION SCHEME ps
AS PARTITION pf
ALL TO ([Primary]);
create table PartitionedTable(id int identity primary key, a int)
on ps(id);
Technically tables aren't partitioned. Tables are comprised of one or more indexes, each of which may be stored on a filegroup or a partition scheme. To see all your partitioned tables (here defined as a table whose main index is stored on a partition scheme), run a query like:
select schema_name(t.schema_id) schema_name, t.name table_name, ds.name partition_scheme
from sys.tables t
join sys.indexes i
on t.object_id = i.object_id
join sys.data_spaces ds
on i.data_space_id = ds.data_space_id
where i.type in (0,1,5) --heap, CI, CCI
and ds.type = 'PS' --partition scheme
The creation of a SQL Server External Table requires remote tables to be made accessible by running code of the following format on the local database:
CREATE EXTERNAL TABLE [TABLE_NAME]
(
[Id] [int] NOT NULL,
[Name] [int] NULL,
)
WITH (DATA_SOURCE = [EXTERNAL_DB_NAME])
I have a quite a few external tables to work with, and am wondering if this functionality can be automated. So, is there a way to create the text for a CREATE TABLE statement from the Information Schema views on the remote database ? This could then be altered and used to populate a dynamic query to be run on the local database.
I am creating a SQL script that will check weather the object is there or not and then do create or alter based on that.
if not exists (select * from sysobjects where name='MyTableName' and xtype='U')
create table MyTableName(
Id int not null,
Name varchar(150) not null
)
Else
--Alter
Now having to do this for a bigger database which has more than 150 table.
Is there any way I can generate this automatically for tables and stored procedures?
As far as I can judge: Your question already includes the anser - NO it can not be done! The Thing is this: you COULD spool the metadata of tables etc. IF the objects existed in an Environment... as you arleady mentioned this is not the case - so how should a function / script / procedure be able to KNOW which columns etc. have to be created for which Table? Or what has to be altered if the table already existed?
When I try to script a table CREATE from a database, it doesn't always script all the columns inside a single CREATE TABLE. Some of the columns will be added via ALTER TABLEs. Why is this? Is SQL server capturing the history of the schema modifications, and any columns added after the initial creation are scripted with ALTERS? The reason this matters is because I'm scripting tables to be used within a VS Database project and it can't handle the ALTER statements.
CREATE TABLE [dbo].[tblPackageType]
(
[PackageTypeId] [int] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
[PackageTypeDesc] [varchar](50) NOT NULL
) ON [PRIMARY]
ALTER TABLE [dbo].[tblPackageType] ADD [EDIcode] [varchar](10) NOT NULL
ALTER TABLE [dbo].[tblPackageType] ADD [EDI211] [varchar](10) NULL
Is SQL server capturing the history of the schema modifications, and any columns added after the initial creation are scripted with ALTERS?
Yes.
If you want to get around this, you can alter the script to be a create including all the needed columns, and after you run it the updated definition will be saved.