MS SQL Server cannot call methods on ntext - sql-server

I am trying to create a view in MS SQL server from a table. The table name is Account_Plan and I am trying to create a view as Account_Plan_vw. While executing the DDL to create the view, I am getting the error as shown below.
Msg 258, Level 15, State 1, Procedure Account_Plan_vw, Line 56
Cannot call methods on ntext
Msg 207, Level 16, State 1, Procedure Account_Plan_vw, Line 22
Invalid column name 'How_the_CU_will_achieve_these_objective2__c'.
The error message shows the column 'How_the_CU_will_achieve_these_objective2__c' as invalid. However, this is a valid column in the Account_Plan table of ntext type.
Can someone help? I just removed the extra columns from the Create view statement.
CREATE VIEW [dbo].[Account_Plan_vw]
AS
SELECT
Results_1.Account__c
,Results_1.How_the_CU_will_achieve_these_objectives__c
,Results_1.How_the_CU_will_achieve_these_objective2__c
FROM
(
SELECT ROW_NUMBER() OVER (PARTITION BY apc1.Account__c ORDER BY apc1.Year__c DESC, apc1.CreatedDate DESC) AS RN_1
,apc1.Account__c
,apc1.How_the_CU_will_achieve_these_objectives__c
,apc1.How_the_CU_will_achieve_these_objective2__c
FROM Account_Plan apc1
INNER JOIN RecordType rtp1
ON apc1.RecordTypeId=rtp1.[Id]
AND rtp1.DeveloperName = 'Account_Plan'
INNER JOIN Account acc1
ON acc1.[Id] = apc1.Account__c
WHERE apc1.Year__c <= YEAR(GETDATE())
) AS Results_1
WHERE RN_1 = 1

NTEXT is deprecated, convert it to NVARCHAR(MAX) instead
see: ntext, text, and image (Transact-SQL)
You should consider altering the table not just casting in the view, but:
CREATE VIEW [dbo].[Account_Plan_vw]
AS
SELECT
results_1.Account__c
, results_1.How_the_CU_will_achieve_these_objectives__c
, results_1.How_the_CU_will_achieve_these_objective2__c
FROM (
SELECT
ROW_NUMBER() OVER (PARTITION BY apc1.Account__c ORDER BY apc1.Year__c DESC, apc1.CreatedDate DESC) AS rn_1
, apc1.Account__c
, apc1.How_the_CU_will_achieve_these_objectives__c
, cast(apc1.How_the_CU_will_achieve_these_objective2__c as nvarchar(max)) as How_the_CU_will_achieve_these_objective2__c
FROM Account_Plan apc1
INNER JOIN RecordType rtp1 ON apc1.RecordTypeId = rtp1.[Id]
AND rtp1.DeveloperName = 'Account_Plan'
INNER JOIN Account acc1 ON acc1.[Id] = apc1.Account__c
WHERE apc1.Year__c <= YEAR(GETDATE())
) AS results_1
WHERE RN_1 = 1

the issue and it was somewhat cryptic to find. The salesforce object had a field as last_peer_review_date__c for which no permissions were given to anybody. As a result, DBAMP user was not able to see the field and hence missed to create this field in SQL server when I used the SF_Replicate command. The create view SQL was created by me couple of weeks ago and it did work at that time. Now, when I used the same SQL, it failed because the SQL had the last_peer_review_date field, but the Account_Plan table does not.
Balaji Pooruli

Related

Accees table attribute in Snowflake CTE

use database DQ_MART;
use schema WORKING;
WITH ASCENDER_EMPLOYEE AS (
**SELECT DISTINCT EMPLOYEE_ID FROM RECONCILLIATION_ASCENDER_WORKER_TIMESHEET**
),
WORKDAY_EMPLOYEE AS (
**SELECT DISTINCT EMPLOYEE_ID FROM RECONCILLIATION_WORKDAY_WORKER_TIMESHEET**
)
SELECT 'Missing employee in Ascender' DQ_RULE_NAME,
RECONCILLIATION_WORKDAY_WORKER_TIMESHEET.EMPLOYEE_ID KEY
FROM WORKDAY_EMPLOYEE WORKDAY
LEFT OUTER JOIN ASCENDER_EMPLOYEE ASCENDER
ON ASCENDER.EMPLOYEE_ID = WORKDAY.EMPLOYEE_ID
;
Hi All, I am bit new to Snowflake SQL CTE. In the above query, I am getting an error, Error: invalid identifier 'RECONCILLIATION_WORKDAY_WORKER_TIMESHEET.EMPLOYEE_ID' (line 16) in this line
RECONCILLIATION_WORKDAY_WORKER_TIMESHEET.EMPLOYEE_ID
The select statements where the same table is accessed runs properly.
Te database and schema where the table resides is set correctly and I do have SELECT grant on the tables.
Is there a scope visibility in Snowflake which is causing the error to occur. Any suggestions will be welcome.
"RECONCILLIATION_WORKDAY_WORKER_TIMESHEET.EMPLOYEE_ID" means the column "EMPLOYEE_ID" in the table "RECONCILLIATION_WORKDAY_WORKER_TIMESHEET".
Your select statement that uses this column is:
SELECT <column list>
FROM WORKDAY_EMPLOYEE WORKDAY
LEFT OUTER JOIN ASCENDER_EMPLOYEE ASCENDER
ON ASCENDER.EMPLOYEE_ID = WORKDAY.EMPLOYEE_ID
which doesn't include a table called RECONCILLIATION_WORKDAY_WORKER_TIMESHEET - which is why you are getting the error

SQL-Server 2005: multi-part identifier could not be bound

I'm working with SQL Server 2005, I'm new to SQL so bear with me.
The following aliased SQL query is giving me the following error:
The multi-part identifier "EquipmentDescription.DESCRIPTION" could not be bound.
SQL:
WITH somerows as
(
SELECT
Mastertable.ID, Mastertable.foo1, Mastertable.foo2,
Mastertable.foo3, EquipmentDescription.DESCRIPTION,
ROW_NUMBER() OVER (ORDER BY Mastertable.ID) AS SeqValue
FROM
EquipmentDescription
LEFT JOIN
MasterTable ON EquipmentDescription.foo1 = MasterTable.foo1
ORDER BY
EquipmentDescription.DESCRIPTION
)
SELECT *
FROM somerows
WHERE SeqValue BETWEEN 0 and 20
Background: Mastertable has 60,000+ records. I'm using WITH...as...etc to request 20 records at a time on the server side.
The DESCRIPTION column of EquipmentDescription for the purposes of design is not included in Mastertable. It is a requirement to include DESCRIPTION in the final select.
Any ideas on what I'm doing wrong?
This works:
SELECT * FROM
(
SELECT
ROW_NUMBER() OVER(ORDER BY ID) AS NUMBER,
Mastertable.ID, Mastertable.foo1, Mastertable.foo2,
Mastertable.foo3, EquipmentDescription.DESCRIPTION,
FROM
EquipmentDescription
RIGHT JOIN
MasterTable ON EquipmentDescription.foo1 = MasterTable.foo1
) AS t_MasterTable
WHERE
NUMBER BETWEEN 0 and 20
ORDER BY ID

SQL FROM with INTO #table not working

I'm dusting off my SQL (or in this case TSQL) and have been writing a stored procedure. Up to this point in my code I can get fine execution with LINQPad as I test my code on the side. However, this particular block, where I have the top scores for each user added to temporary table #QuizScores is delivering a generic syntax error
Incorrect syntax near the keyword 'SELECT'
I've tried to move all the pieces of this statement around but seem to be failing. Any insight you might be able to share?
Here's the statement:
SELECT *
INTO #QuizScores
FROM (SELECT ROW_NUMBER()
OVER (
PARTITION BY UserID
ORDER BY count(#QuizResponses.IsCorrectAnswer) DESC) AS
RowNumber,
UserID,
count(#QuizResponses.IsCorrectAnswer) AS
Points
FROM #QuizResponses)
JOIN #QuizResponses
ON #QuizResponses.QuizAttemptID = #QuizAttemptList.QuizAttemptID;
i have taken some assumed data inserted into temp table and modified syntax according to your requirement
IF OBJECT_ID('tempdb..#QuizResponses') IS NOT NULL
DROP TABLE #QuizResponses
GO
IF OBJECT_ID('tempdb..#QuizScores') IS NOT NULL
DROP TABLE #QuizScores
CREATE TABLE #QuizResponses (UserId int,correctanswer int,IsCorrectAnswer varchar(1),QuizAttemptID INT)
INSERT INTO #QuizResponses (UserId,correctanswer,IsCorrectAnswer)values (1,1,'Y',2),(2,1,'N',2)
select P.RowNumber,P.Points,P.UserId INTO #QuizScores from (
SELECT ROW_NUMBER() OVER (PARTITION BY a.UserID
ORDER BY count(a.IsCorrectAnswer) DESC) AS RowNumber, a.UserID ,
count(a.IsCorrectAnswer) AS Points
FROM #QuizResponses a
JOIN #QuizResponses S ON S.UserId = A.UserId
group by a.UserId )P
[EDIT: This reflects an earlier revision of the question.]
From MSDN:
SELECT…INTO creates a new table in the default filegroup
Emphasis added.

Using Row_Number() Over (Order By Column) When Column is in Different DB Returns Error

I am submitting the following query in Sql Server (2008)
WITH query AS (SELECT TOP 100 PERCENT
ROW_NUMBER() OVER
(ORDER BY [tbl2].[col2] ASC) AS TableRowNumber ,
[tbl1].[col1] ,
[tbl2].[col2]
FROM [db1].[dbo].[tbl1] AS [tbl1]
JOIN [db2].[dbo].[tbl2] AS [tbl2]
ON [tbl1].[id] = [tbl2].[id])
SELECT
*
FROM query
WHERE TableRowNumber BETWEEN 1 AND 15
ORDER BY TableRowNumber ASC
When this query is run, it returns the following error message:
Msg 207, Level 16, State 1, Line 3
Invalid column name 'col2'.
The sql join itself runs fine (when run as a normal query. The issue seems to be with using the Row_Number() over (Order By COLUMN) when COLUMN is in a different database from the FROM table of the query.
If I would change line 3 to read (Order By [tbl1].[col1] ASC) then it runs without any issues. The error only happens when the sort column is in a different DB.
Does anyone know why this is happening? Any suggestions on how to fix this?
This works for me no problem:
SELECT a.name, b.object_id, rn = ROW_NUMBER() OVER (ORDER BY b.object_id DESC)
FROM sys.columns AS a
INNER JOIN tempdb.sys.objects AS b
ON a.object_id = b.object_id;
So I suspect there is some other issue going on (e.g. col2 really doesn't exist). Also I noticed that you are calling the thing tb2 and tbl2 - is it possible you have both a tb2 and a tbl2 in the other database, and you're referencing the wrong one?
EDIT I created this:
CREATE DATABASE db1;
GO
USE db1;
GO
CREATE TABLE dbo.tbl1(ID INT, col1 INT);
GO
INSERT dbo.tbl1 SELECT 1, 5
UNION ALL SELECT 2, 10;
GO
CREATE DATABASE db2;
GO
USE db2;
GO
CREATE TABLE dbo.tbl2(ID INT, col2 INT);
GO
INSERT dbo.tbl2 SELECT 1, 9
UNION ALL SELECT 2, 4;
GO
USE db1;
GO
Then ran your query in the context of db1. It ran fine. So for the last time I will suggest that there is something you're not telling us about the schema, or perhaps the fact that you've obfuscated the names (and already had to correct one typo from doing so) has obfuscated something too much even for you...

SQL Server: problems with max and IN clause

I have this table in SQL Server 2008:
create table [hFUNDASSET]
(
[hFundAssetID] numeric(19,0) identity not null,
[fundAssetID] numeric(19,0) not null,
[modified] datetime not null,
primary key ([hFundAssetID])
);
Its a history table, the goal is to get the closest hFundAssetID based on a given max modified column timestamp, for every distinct fundAssetID.
The following query gets the correct latest modified for each fundAssetID:
select max(modified), fundAssetID
from hFundAsset
where fundAssetID IN
(
select distinct (fundAssetID)
from hFundAsset where modified < 'April 20, 2010 11:13:00'
)
group by fundAssetID;
I can't put the hFundAssetID in the select clause without it being in the group by , which would return extra rows. Somehow, I need the hFundAssetID matching each one of the modified, fundAssetID pairs returned in the above query, or equivalent. But SQL Server doesn't allow multiple values for the IN clause, according to their docs:
"subquery - Is a subquery that has a result set of one column.
This column must have the same data type as test_expression."
Googling shows that 'exists' and joins are typically used with mssql in these cases, but I've tried that using 'max' and 'group by' and I'm having problems getting it to work. Any help appreciated.
Try this:
WITH qry AS
(
SELECT a.*,
RANK() OVER(PARTITION BY fundAssetID ORDER BY modified DESC) rnk
FROM hFundAsset a
WHERE modified < 'April 20, 2010 11:13:00'
)
SELECT *
FROM qry
WHERE rnk = 1

Resources