TSQL Computed column limitations - sql

CREATE TABLE [dbo].[MembershipModule](
[Id] [uniqueidentifier] ROWGUIDCOL NOT NULL,
[ParentId] [uniqueidentifier] NULL,
[TargetId] [int] NULL,
[WebContentId] [uniqueidentifier] NULL,
[Name] [varchar](35) NOT NULL,
[NameUpper] AS (isnull(upper([Name]),'')) PERSISTED NOT NULL,
[UriPrefix] [varchar](max) NULL,
[UriText] [varchar](max) NULL,
[UriComputed] AS ??? PERSISTED,
[Description] [varchar](100) NULL,
[Created] [date] NOT NULL,
[Modified] [datetime2](7) NOT NULL,
[MenuItem] [bit] NOT NULL,
[Enabled] [bit] NOT NULL,
[Position] [smallint] NULL,
CONSTRAINT [PK_MembershipModule] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
So far the UriComputed field is computed like this:
lower(replace(isnull([UriPrefix],'/')+coalesce([UriText],[Name]),' ','-'))
This produces output like the following
Now, I'd want to terminate all UriComputed values with '/'. This would be easily accomplished by adding + '/' to the computed field, except for the fact that "textless" uris, would be terminated like //, which I don't want happening.
since the sql I can put into a computed field is quite limited (and I don't really know the extents of these limitations) I thought I'd ask here how to add this.
basically I want the output in the image to be
/a/login/
/a/announcements/
/a/
/
my closest attempt at doing this has been:
isnull(convert(varchar(MAX),nullif(len(coalesce([UriText],[Name])),0)),'/')
Which does kind of a mess, and adds a number if it should terminate in '/', and adds '/' when it should, what I'd need is the opposite ( that is, '/' when the length is 0, '' otherwise)
If there's an inline if or something like that I could use that would basically be it, but I don't know about that.
Thank you!

This worked for me:
[UriComputed] AS (CASE
WHEN RIGHT(lower(replace(isnull([UriPrefix],'/')+coalesce([UriText],[Name]),' ','-')), 1) = '/' THEN
lower(replace(isnull([UriPrefix],'/')+coalesce([UriText],[Name]),' ','-'))
ELSE
lower(replace(isnull([UriPrefix],'/')+coalesce([UriText],[Name]),' ','-')) +'/'
END) PERSISTED,
Full CREATE TABLE statement:
CREATE TABLE [dbo].[MembershipModule](
[Id] [uniqueidentifier] ROWGUIDCOL NOT NULL,
[ParentId] [uniqueidentifier] NULL,
[TargetId] [int] NULL,
[WebContentId] [uniqueidentifier] NULL,
[Name] [varchar](35) NOT NULL,
[NameUpper] AS (isnull(upper([Name]),'')) PERSISTED NOT NULL,
[UriPrefix] [varchar](max) NULL,
[UriText] [varchar](max) NULL,
[UriComputed] AS (CASE
WHEN RIGHT(lower(replace(isnull([UriPrefix],'/')+coalesce([UriText],[Name]),' ','-')), 1) = '/' THEN
lower(replace(isnull([UriPrefix],'/')+coalesce([UriText],[Name]),' ','-'))
ELSE
lower(replace(isnull([UriPrefix],'/')+coalesce([UriText],[Name]),' ','-')) +'/'
END) PERSISTED,
[Description] [varchar](100) NULL,
[Created] [date] NOT NULL,
[Modified] [datetime2](7) NOT NULL,
[MenuItem] [bit] NOT NULL,
[Enabled] [bit] NOT NULL,
[Position] [smallint] NULL)

Related

Speed up Group By and count SQL query

I am counting/grouping about 1.3 million records with the statement below. The query works but is taking about a minute and a half, which is way too long for my application.
The goal is to get a one each (no duplicates) of the listed fields. The current query returns around 846 rows. I don't have any indexes so far, nor do I know much about adding them.
SELECT
[OfferId]
,[Name]
,COUNT([Name]) AS 'Count'
,[Offer]
,[Title]
,[Text]
,[Amount]
,[Start]
,[End]
,[Image]
,[ImageText]
,[Type]
,[Disclaimer]
,[Link]
,[Status]
FROM
ClientDB.[dbo].[Offers]
GROUP BY
[OfferId]
,[Name]
,[Offer]
,[Title]
,[Text]
,[Amount]
,[Start]
,[End]
,[Image]
,[ImageText]
,[Type]
,[Disclaimer]
,[Link]
,[Status]
Table structure (not sure how to index this to make it faster):
CREATE TABLE [dbo].[Offers]
(
[ID] [int] IDENTITY(1,1) NOT NULL,
[Company] [nvarchar](max) NULL,
[Property] [nvarchar](max) NULL,
[Account] [int] NULL,
[OfferID] [nvarchar](50) NULL,
[Offer] [nvarchar](50) NULL,
[Title] [nvarchar](30) NULL,
[Text] [nvarchar](max) NULL,
[AwardCode] [nvarchar](150) NULL,
[Amount] [decimal](18, 2) NULL,
[Start] [datetime] NULL,
[End] [datetime] NULL,
[Image] [nvarchar](max) NULL,
[ImageText] [nvarchar](250) NULL,
[Type] [nvarchar](50) NULL,
[CampaignTier] [nvarchar](50) NULL,
[Name] [nvarchar](max) NULL,
[Disclaimer] [nvarchar](max) NULL,
[Status] [nvarchar](100) NULL,
CONSTRAINT [PK_Offers]
PRIMARY KEY CLUSTERED ([ID] ASC)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
I added indexes to the fields I was grouping together and this cut the time from a minute and a half to 1 second.

Slow and fast comparison in Tablediff.exe

I am using tablediff.exe to compare two SQL tables, on two servers one is local and the other is remote.
My first table is Table1 (380,000 records)
My second Table is Table2 (290,000 records)
When I compare local Table1 to remote Table1 it takes about 6 minutes .
When I compare local Table2 to remote Table2 it takes about forever and no result (Table2 has less records than Table1)
I deleted a column of type image from Table2, but the problem still there.
What may be the problem ?
NOTE: TABLE2 Structure
[ID] [varchar](10) NOT NULL,
[Title] [nvarchar](10) NULL,
[FName] [nvarchar](255) NULL,
[MName] [nvarchar](25) NULL,
[LName] [nvarchar](25) NULL,
[MotherName] [nvarchar](25) NULL,
[DOB] [datetime] NULL,
[BirthPlace] [nvarchar](50) NULL,
[Gender] [varchar](1) NULL,
[MaritalStatus] [nvarchar](25) NULL,
[ChildrenNo] [varchar](50) NULL,
[Occupation] [nvarchar](25) NULL,
[NationalityID] [varchar](3) NULL,
[Address] [nvarchar](255) NULL,
[Telephone] [varchar](20) NULL,
[Mobile] [varchar](20) NULL,
[Email] [varchar](50) NULL,
[BloodGroup] [varchar](3) NULL,
[RecieveSMS] [bit] NULL,
[FileNo] [nvarchar](250) NULL,
[PrimaryPhysician] [varchar](10) NULL,
[IqamaID] [varchar](10) NULL,
[EmergencyContactName] [nvarchar](50) NULL,
[EmergencyContactRelation] [nvarchar](50) NULL,
[EmergencyContactNo] [nvarchar](50) NULL,
[ProfilePicture] [image] NULL,
[Status] [bit] NULL,
[SecondaryTelNo] [varchar](20) NULL,
[SecondaryMobileNo] [varchar](20) NULL,
[Notes] [nvarchar](255) NULL,
[BankID] [varchar](10) NULL,
[IBANNo] [nvarchar](50) NULL,
[isWalkIn] [bit] NULL CONSTRAINT [DF_Patients_isWalkIn] DEFAULT ((0)),
[AR_FName] [nvarchar](255) NULL,
[AR_MName] [nvarchar](25) NULL,
[AR_LName] [nvarchar](25) NULL,
[AR_MotherName] [nvarchar](25) NULL,
[isVIP] [bit] NULL,
[FirstVisitDate] [varchar](255) NULL,
[EmploymentID] [varchar](20) NULL,
[ImagePath] [nvarchar](250) NULL,
[PatientLevel] [int] NOT NULL CONSTRAINT [DF_Patients_PatientLevel] DEFAULT ((1)),
CONSTRAINT [PK_Patients] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

SQL AZURE : An error occurred while executing GlobalQuery operation: Large object column support is limited to only nvarchar(max) data type

Sql azure query: after creating external table i run select query to get data from external table but this error occures!
i removed all columns with datatype = nvarchar(max) but also the problem have not been solved yet!
Code to create external table:
CREATE External TABLE [dbo].[tbl_threads_controlPanel_v](
[thread_id] [varchar](6) NOT NULL,
[thread_desc_criteria] [varchar](300) NOT NULL,
[thread_desc_formula] [varchar](300) NOT NULL,
[thread_type] [char](1) NOT NULL,
[detectType] [char](1) NOT NULL,
[detailed_qry] [nvarchar](300) NULL,
[bottomup_qry] [nvarchar](300) NULL,
[period_desc] [char](1) NULL,
[period_value] [int] NULL,
[period_value_range] [varchar](50) NULL,
--[cond_attribute] [nvarchar](max) NULL,
[cond_min_max_limit] [varchar](30) NULL,
[cond_desc] [varchar](60) NULL,
[active] [char](1) NULL,
[mature] [char](1) NULL,
[pkg_run] [char](1) NULL,
[thread_index] [int] NULL,
[thread_weight] [numeric](12, 11) NULL,
[thread_noti_type] [char](1) NULL,
[notif_id] [varchar](9) NULL,
[amt_type] [nchar](5) NULL--,
--[report_Columns] [nvarchar](max) NULL,
--[OS_Columns] [nvarchar](max) NULL
)
with(DATA_SOURCE = MyElasticDBQueryDataSrc3)
And this is the select query:
select * from dbo.[tbl_threads_controlPanel_v]
Please help ..
Thanks in advance.
UPDATE 2:
This is original tbl_threads definition
USE [DB_IFDPS_ControlPanel]
GO
/****** Object: Table [dbo].[tbl_threads] Script Date: 10/26/2016 8:51:09 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[tbl_threads](
[thread_id] [varchar](6) NOT NULL,
[thread_desc_criteria] [varchar](300) NOT NULL,
[thread_desc_formula] [varchar](300) NOT NULL,
[thread_type] [char](1) NOT NULL,
[detectType] [char](1) NOT NULL,
[detailed_qry] [text] NULL,
[bottomup_qry] [text] NULL,
[period_desc] [char](1) NULL,
[period_value] [int] NULL,
[period_value_range] [varchar](50) NULL,
[cond_attribute] [nvarchar](max) NULL,
[cond_min_max_limit] [varchar](30) NULL,
[cond_desc] [varchar](60) NULL,
[active] [char](1) NULL,
[mature] [char](1) NULL,
[pkg_run] [char](1) NULL,
[thread_index] [int] NULL,
[thread_weight] [numeric](12, 11) NULL,
[thread_noti_type] [char](1) NULL,
[notif_id] [varchar](9) NULL,
[amt_type] [nchar](5) NULL,
[report_Columns] [nvarchar](max) NULL,
[OS_Columns] [nvarchar](max) NULL,
CONSTRAINT [PK_tbl_threads] PRIMARY KEY CLUSTERED
(
[thread_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
It looks like this is not the whole story.
The problem seems to be not with the varchar(max) columns but with a column that is defined somewhere else as a LOB but in the external table is defined otherwise than varchar(max)
Thanks for adding the relevant DDL.
The issue is most likely with the text columns
CREATE External TABLE [dbo].[tbl_threads_controlPanel_v](
...
[detailed_qry] [nvarchar](300) NULL,
[bottomup_qry] [nvarchar](300) NULL,
...
CREATE TABLE [dbo].[tbl_threads]
...
[detailed_qry] [text] NULL,
[bottomup_qry] [text] NULL,
...
P.s.
https://msdn.microsoft.com/en-us/library/ms187993.aspx
IMPORTANT! ntext, text, and image data types will be removed in a future version of SQL Server. Avoid using these data types in new development work, and plan to modify applications that currently use them. Use nvarchar(max), varchar(max), and varbinary(max) instead.

SQL query taking 1 minute and 35 seconds

I am executing the query on SQL server on hosting and it is taking 1 minute and 35 seconds. And the no, of rows of retrieval are 18000. Still it is taking too much time. Query is
select ID,
FirstName,
LastName,
Branch,
EnquiryID,
Course,
College,
Mobile,
ExamID,
EntranceID,
Entrance,
Venue,
RegNo,
VenueID,
Exam,
Gender,
row_number() over (partition by EnquiryID order by ID asc) as AttemptNO
from AGAM_View_AOPList
order by EnquiryID
TABLE SCHEMAS
CREATE TABLE [dbo].[AGAM_AceOFPace](
[ID] [int] IDENTITY(1,1) NOT NULL,
[EnquiryID] [int] NULL,
[FirstName] [nvarchar](100) NULL,
[MiddleName] [nvarchar](100) NULL,
[LastName] [nvarchar](100) NULL,
[BranchID] [int] NULL,
[Branch] [nvarchar](100) NULL,
[CourseID] [int] NULL,
[ExamID] [int] NULL,
[Exam] [nvarchar](200) NULL,
[EntranceID] [int] NULL,
[Entrance] [nvarchar](200) NULL,
[RegNo] [nvarchar](200) NULL,
[EntranceCode] [nvarchar](100) NULL,
[ExamDate] [nvarchar](50) NULL,
[UserID] [nvarchar](100) NULL,
[EntranceFees] [numeric](18, 2) NULL,
[VenueID] [int] NULL,
[Venue] [nvarchar](max) NULL,
[ChequeNumber] [nvarchar](50) NULL,
[Bank] [nvarchar](100) NULL,
[CreatedDate] [datetime] NULL,
CONSTRAINT [PK_AGAM_AceOFPace] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[AGAM_AceOFPace] WITH CHECK ADD CONSTRAINT [FK_AGAM_AceOFPace_AGAM_Inquiry] FOREIGN KEY([EnquiryID])
REFERENCES [dbo].[AGAM_Inquiry] ([ID])
GO
ALTER TABLE [dbo].[AGAM_AceOFPace] CHECK CONSTRAINT [FK_AGAM_AceOFPace_AGAM_Inquiry]
GO
SECOND TABLE
CREATE TABLE [dbo].[AGAM_Inquiry](
[ID] [int] IDENTITY(1,1) NOT NULL,
[RegNo] [nvarchar](200) NULL,
[BranchID] [int] NULL,
[Category] [nvarchar](100) NULL,
[CourseID] [int] NULL,
[EntranceFees] [numeric](18, 2) NULL,
[EntranceID] [int] NULL,
[UserID] [nvarchar](50) NULL,
[Status] [nvarchar](50) NULL,
[ReminderDate] [datetime] NULL,
[Reminder] [nvarchar](150) NULL,
[Mobile] [nvarchar](50) NULL,
[Email] [nvarchar](50) NULL,
[FirstName] [nvarchar](50) NULL,
[MiddleName] [nvarchar](50) NULL,
[LastName] [nvarchar](50) NULL,
[Landline] [nvarchar](50) NULL,
[Address] [nvarchar](100) NULL,
[DOB] [datetime] NULL,
[Gender] [nvarchar](50) NULL,
[PfBatchTime] [nvarchar](50) NULL,
[SourceOfInquiry] [nvarchar](50) NULL,
[ExStudentID] [int] NULL,
[InquiryDate] [datetime] NULL,
[ReceiptNumber] [nvarchar](50) NULL,
[RawID] [int] NULL,
[Deleted] [int] NULL,
[CreatedBy] [nvarchar](50) NULL,
[CreatedDate] [datetime] NULL,
[LastModifiedBy] [nvarchar](50) NULL,
[LastModifiedDate] [datetime] NULL,
[College] [nvarchar](150) NULL,
[Qualification] [nvarchar](150) NULL,
[RptNo] [nvarchar](100) NULL,
CONSTRAINT [PK_AGAM_Inquiry] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[AGAM_Inquiry] WITH CHECK ADD CONSTRAINT [FK_AGAM_Inquiry_AGAM_Branch] FOREIGN KEY([BranchID])
REFERENCES [dbo].[AGAM_Branch] ([ID])
GO
ALTER TABLE [dbo].[AGAM_Inquiry] CHECK CONSTRAINT [FK_AGAM_Inquiry_AGAM_Branch]
GO
ALTER TABLE [dbo].[AGAM_Inquiry] WITH CHECK ADD CONSTRAINT [FK_AGAM_Inquiry_AGAM_Course] FOREIGN KEY([CourseID])
REFERENCES [dbo].[AGAM_Course] ([ID])
GO
ALTER TABLE [dbo].[AGAM_Inquiry] CHECK CONSTRAINT [FK_AGAM_Inquiry_AGAM_Course]
GO
ALTER TABLE [dbo].[AGAM_Inquiry] WITH CHECK ADD CONSTRAINT [FK_AGAM_Inquiry_AGAM_Users] FOREIGN KEY([UserID])
REFERENCES [dbo].[AGAM_Users] ([UserID])
GO
ALTER TABLE [dbo].[AGAM_Inquiry] CHECK CONSTRAINT [FK_AGAM_Inquiry_AGAM_Users]
GO
Can you try with changing the view to this?
SELECT TOP (100) PERCENT
AP.ID,
AP.FirstName,
AP.LastName,
AP.Branch,
AP.EnquiryID,
AC.Name,
AI.College,
AI.Mobile,
AP.ExamID,
AP.EntranceID,
AP.RegNo,
AP.VenueID,
AP.Exam,
AI.Gender,
AP.BranchID,
AP.CourseID,
AP.CreatedDate,
AI.Status,
AP.Entrance,
AP.Venue
FROM dbo.AGAM_AceOFPace AS AP
INNER JOIN dbo.AGAM_Inquiry AS AI ON AI.ID = AP.EnquiryID
INNER JOIN dbo.AGAM_Course as AC on AC.ID = AP.CourseId
ORDER BY AP.EnquiryID
Do you have an index on EnquiryId and CourseID?
Seeing as you are joining, ordering and partitioning by it you really should.
CREATE INDEX IDX_AGAM_AceOFPace_EnquiryID
ON AGAM_AceOFPace (EnquiryID)
CREATE INDEX IDX_AGAM_AceOFPace_CourseID
ON AGAM_AceOFPace (CourseID)

SQL syntax conversion error with mysterious data

I'm getting this message in an SQL Server 2000 stored procedure:
Syntax error converting the varchar value '...............' to a
column of data type int.
Basically I'm trying to insert data and use it in an existing stored procedure, so its possible that I"m inserting bad data, but I'm not inserting '...............' anywhere, and I can't find that anywhere related.
Trouble is, I can't find '...............' anywhere in the table I'm querying. How can I find the source of this error?
EDIT
Part of the Stored Procedure throwing error:
Select #iFromLocationID = IsNull(RecID,0)
From InventoryLocations
Where LocItemNumber = #vItemNumber
And IsNull(SkidNumber,'') <> ''
And Cast(SkidNumber as int) = #iSkidFlagID
And Warehouse = #cFromWarehouse
And Aisle = #cFromAisle
And Slot = #cFromSlot
And locLevel = #cFromLevel
And Bin = #cFromBin
Table Schema for InventoryLocations:
CREATE TABLE [dbo].[InventoryLocations](
[recid] [int] IDENTITY(1,1) NOT NULL,
[LocItemNumber] [char](16) NOT NULL,
[WareHouse] [char](2) NOT NULL,
[Aisle] [char](3) NOT NULL,
[Slot] [char](3) NOT NULL,
[locLevel] [char](2) NOT NULL,
[Bin] [char](2) NOT NULL,
[Extra] [char](2) NOT NULL,
[LocNumber] [char](2) NOT NULL,
[RollNumber] [char](20) NOT NULL,
[QuickRoll] [int] NOT NULL,
[SkidNumber] [char](15) NOT NULL,
[RollsInStock] [int] NOT NULL,
[LocQtyOnHand] [float] NOT NULL,
[LocQtyOnOrder] [float] NOT NULL,
[LocQtyCommited] [float] NOT NULL,
[TotalReceived] [float] NOT NULL,
[TotalIssued] [float] NOT NULL,
[TotalDollars] [float] NOT NULL,
[Capacity] [float] NOT NULL,
[AvailableSpace] [float] NOT NULL,
[bkey0] [char](30) NULL,
[bkey1] [char](30) NULL,
[bkey2] [char](30) NULL,
[bkey3] [char](14) NULL,
[LastPhysicalCountDate] [datetime] NULL,
[LastCycleCountDate] [datetime] NULL,
[EnteredBy] [varchar](50) NULL,
[EnteredDateTime] [datetime] NULL,
CONSTRAINT [IX_InventoryLocations_1] UNIQUE NONCLUSTERED
(
[LocItemNumber] ASC,
[WareHouse] ASC,
[Aisle] ASC,
[Slot] ASC,
[locLevel] ASC,
[Bin] ASC,
[Extra] ASC,
[RollNumber] ASC,
[SkidNumber] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
If you're building dynamic sql, try printing the query before executing it. If not, try running the queries in the stored procedure individually in an editor. If you have an INSERT..SELECT, try only running the SELECT part of the query.
This has to be the issue:
And IsNull(SkidNumber,'') <> ''
And Cast(SkidNumber as int) = #iSkidFlagID
Try running the following and see if you get an error:
SELECT CAST(SkidNumber as INT)
FROM InventoryLocations
Since #iSkidFlagID is an INT and SkidNumber is CHAR(15) it has to be where the error is occurring.
It looks like a null value was causing SQL to return the '..................'. I've changed the values I'm inserting and now I don't get that error anymore.