|
|
|
|
|
|
Question of the Day |
Today's question (by Junior Galvão - MVP): | |
Self-numbered primary key creation | |
I have a database, called Stock, and I have the need to create a new table, called Orders, according to the code below:
-- Accessing the Stock Database -- Use Stocks Go -- Creating the Orders Table -- Create Table Orders (ID Int IDENTITY(1,1) Not Null Primary Key, CustomerID Int Not Null, SalesPersonID Varchar(30) Not Null, Quantity smallint Not Null, NumericValue numeric(18, 2) Not Null, Today Date Not Null) GoRight after creating the table, you choose to use some recursive CTEs the amount of 100,000 rows of records in the Orders table: ;WITH E1(N) AS (SELECT 1 FROM (VALUES (1),(1),(1),(1),(1),(1),(1),(1),(1),(1))E0(N)), E10(N) AS (SELECT 1 FROM E1 a, E1 b, E1 c, E1 d, E1 e, E1 f, E1 g, E1 h, E1 i, E1 j), TallY(N) AS (SELECT TOP(@RowCount) ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM E10) INSERT INTO dbo.Orders (CustomerID, SalesPersonID, Quantity, NumericValue, Today) SELECT CustomerID = @RowCount+1-t.N ,SalesPersonID = SUBSTRING(ca.Texto,ABS(CHECKSUM(NEWID())%126)+1,2) + SUBSTRING(ca.Texto,ABS(CHECKSUM(NEWID())%124)+1,4) + SUBSTRING(ca.Texto,ABS(CHECKSUM(NEWID())%124)+1,4) ,Quantity = ABS(CHECKSUM(NEWID())%1000) ,NumericValue = RAND(CHECKSUM(NEWID()))*100+5 ,Today = DATEADD(dd,ABS(CHECKSUM(NEWID())%1000),GETDATE()) FROM TallY t CROSS APPLY (SELECT '0123456789@ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyzŽŸ¡ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖÙÚÛÜÝàáâãäåæçèéêëìíîïðñòóôõöùúûüýÿ')ca(Texto) SELECT ID, CustomerID, SalesPersonID, Quantity, NumericValue, Today FROM Orders Go | |
Think you know the answer? Click here, and find out if you are right. |
Yesterday's Question of the Day (by Steve Jones - SSC Editor) |
Storing the Timezone What datatype(s) are used to store a date and time with the time zone information? Answer: datetimeoffset Explanation: Only datetimeoffset stores time zone information. Ref: Datetimeoffset - https://docs.microsoft.com/en-us/sql/t-sql/data-types/datetimeoffset-transact-sql?view=sql-server-ver15 |
Database Pros Who Need Your Help |
Here's a few of the new posts today on the forums. To see more, visit the forums. |
SQL Server 2017 - Administration |
Could database with compat level 100 support columnstore index? - Hi everyone. I asked this question previously on other forum, but didn't get satisfied answer. I hope I could find the answer here. So, I have an instance which runs on SQL Server 2017 Developer Edition instance. I restored database ContosoRetailDW which had compatibility level 100. After restore operation, I run the following query: CREATE […] |
Changing datatype in a column - Do I have to rebuild the Clustered columnstore index when we modify datatype in a column? Thanks! |
High buffer size for a table could mean either it has incorrect indexes or ? - or simply that the table is USED A LOT, right? -- Breaks down buffers used by current database by object (table, index) in the buffer cache (Query 51) SELECT OBJECT_NAME(p.[object_id]) AS [ObjectName], p.index_id, COUNT(*)/128 AS [Buffer size(MB)], COUNT(*) AS [BufferCount], p.data_compression_desc AS [CompressionType] FROM sys.allocation_units AS a WITH (NOLOCK) INNER JOIN sys.dm_os_buffer_descriptors AS b WITH […] |
SQL Server 2016 - Administration |
Page Verify = Checksum - Hi I'm belated changing the page verification option for a database from NONE to CHECKSUM. I'm aware this only affects future writes to the database, and existing pages won't have that checksum calculated until they are updated or the relevant tables or indexes are rebuilt. My question is: would an ALTER INDEX ... REORGANIZE also […] |
Audit connection which are connecting to a server using AG listener. - I am in a middle of removing a server which is part of AG and someone has asked me to see if there are users, connecting to that specific server using a listener name so I used a DMV to capture all that information. The script is below SELECT es.login_name ,es.program_name ,login_time ,ec.client_net_address ,ec.client_tcp_port ,agl.dns_name […] |
Development - SQL Server 2014 |
How would I get the last record of a self-referential table? - This summer I authored a report that referenced nine tables in a query. This is against an older database. This is for a pharmacy application that someone back when wrote originally in a Microsoft Access application. Along the way somebody upgraded the database from the original .MDB to a SQL 2012 database, where it currently […] |
SQL 2012 - General |
Errors enabling and disabling Change Data Capture for 1 table - Hello, when we try to enable CDC on a table that is dropped and recreated each night, we get the below error: Msg 22926, Level 16, State 1, Procedure sp_cdc_verify_capture_instance, Line 36 [Batch Start Line 0]Could not create a capture instance because the capture instance name 'dbo_appt_slots' already exists in the current database. Specify an […] |
SQL Server 2012 - T-SQL |
Performance Issue - I have a SQL server on a HyperV cloud. The server harddrive is stored on the SAN. I have separate LUNS (drive letters) to the mdf, ldf and tempdb files. When I run "select * from product" on my local copy, it takes 2 seconds. When I run the same query on the sql server […] |
SQL Server 2019 - Administration |
Key locks on ghosted records - We're curious about locks that might revolve around ghost records that have been deleted. Does anyone have an explanation for this scenario? Given the following table: CREATE TABLE [dbo].[TEST_TABLE]( [column_A] [nvarchar](30) NOT NULL, [column_B] [tinyint] NOT NULL, [column_C] [bigint] NOT NULL, [column_D] [int] NOT NULL, CONSTRAINT [i01TESTTABLEKEY] PRIMARY KEY CLUSTERED ( [column_A] ASC, [column_B] ASC, […] |
SQL Server 2019 - Development |
SQL Full text query too slow - I have some Full Text Catalogs and since some time ago, the query time really slow down, and the only solution I found until now is to rebuild the catalogs, sometimes more than once a day with command: ALTER FULLTEXT CATALOG TABLE1 REBUILD Why is necessary to rebuild the catalogs? There are other solutions for […] |
SSRS 2012 |
Formatting parameter pane - I have over 30 parameters in my report. Is there a way to force the parameters into a particular format rather than having them line up in two columns when the report is run? |
Powershell |
Scanning to find sql instances/servers - We use MAPS tool to scan through our domain and then list out our sql servers/instances. How can this be achieved using powershell ? Thanks |
SSDT |
CDC Source tool in SSIS - varchar data mapping issue - I'm trying to introduce myself to the change data capture items in the SSIS Toolbox and have hit a problem with the CDC Source tool. I've been using this tutorial as a guide: https://www.mssqltips.com/sqlservertip/5815/sql-server-integration-services-ssis-cdc-tasks-for-incremental-data-loading/ So I have a source table with varchar data types in SQL Server, but in the CDC Source Advanced Editor it […] |
SSIS performance issue: Data load from sqlserver2016 (EC2) - Mysql8.0 (RDS) - Hi, I am trying to Full Load from Table on sqlserver(2016) to a table on Aurora My Sql database (RDS).The table has 1M records and taking 3 hrs to load it when I run it as a job on server. I am using ODBC 32 bit driver and ADO.net Provider on the ETL side. The […] |
Integration Services |
Find MAX value from multiple datetime columns - This may have an easy resolution but I'm having difficulty finding it. I'm creating a package that needs to take records that are new or updated since the last time the job run. I have 2 DateTime columns Insert_date and Last_Updated. I need records for newly inserted records and modified/last_updated records. How do I accomplish […] |
| |
©2019 Redgate Software Ltd, Newnham House, Cambridge Business Park, Cambridge, CB4 0WZ, United Kingdom. All rights reserved. webmaster@sqlservercentral.com |