Line break in T-sql (Char(13)) is not working - c#

I am using a T-sql (Sql server 2008) stored procedure which returns a custmized values based on many business conditions. (This must be done on database because i have a generic call to many stored procedures from C# code).
My issue here is with the line-break. I use in my code Char(13);Char(13) + char(10); char(10)... but it doesn't work.
NB: If i test an INSERT statment it works but in the SELECT statment no line break ,it generates a White space (Done on a simple query).
Bellow my code
ALTER PROCEDURE [dbo].[getDebtorAddress] (
#idFolder int
)
AS
BEGIN
DECLARE
#type bit ,
#firstName varchar(100) ,
#familyName varchar(100) ,
#raisonSocial varchar(100),
#jeuneFille varchar(100) ,
#saleName varchar (100) ,
#enseigne varchar (100) ,
#sigle varchar (100) ,
#address varchar (max),
#lieuDit varchar(100) ,
#postalCode varchar (100),
#city varchar (100),
#country varchar (100),
#finalAddress nvarchar(max)
SET NOCOUNT ON;
SELECT DISTINCT
#firstName = Actor.M_EN_NOM,
#familyName = Actor.M_EN_PRENOM1,
#raisonSocial = Actor.M_EN_RAISONSOCIALE,
#jeuneFille = Actor.M_EN_NOMJEUNEFILLE,
#saleName = Actor.M_EN_NOMCOMMERCIAL,
#enseigne = Actor.M_EN_ENSEIGNE,
#sigle = Actor.M_EN_SIGLE,
#address = ActorCP.M_PR_CP_ADRESSE,
#lieuDit = ActorCP.M_PR_CP_LIEUDIT,
#postalCode = PostalCode.C_PA_CP_CODEPOSTALE,
#city = PostalCode.C_PA_CP_VILLE,
#country = Country.C_PA_PA_LIBELLE,
#type = Actor.M_EN_TYPE
FROM M_EN Actor
LEFT OUTER JOIN M_DE Debtor ON Actor.M_EN_ID =Debtor.M_EN_ID
LEFT OUTER JOIN M_DO Folder ON Debtor.M_DE_ID= Folder.M_DE_ID
LEFT OUTER JOIN M_PR_CP ActorCP ON Actor.M_EN_ID = ActorCP.M_EN_ID
LEFT OUTER JOIN C_PA_CP PostalCode ON ActorCP.C_PA_CP_ID = PostalCode.C_PA_CP_ID
LEFT OUTER JOIN C_PA_PA Country ON ActorCP.C_PA_PA_ID = Country.C_PA_PA_ID
WHERE Folder.M_DO_ID = #idFolder
if(#type=0)
begin
if (#firstName is not Null and #firstName !='')
set #finalAddress= #firstName
if(#familyName is not Null and #familyName != '')
set #finalAddress = #finalAddress + ' ' + #familyName +CHAR(13)
if(#jeuneFille is not null and #jeuneFille !='' )
set #finalAddress=#finalAddress + ' ' + #jeuneFille + CHAR(13)
if(#address is not null and #address != '')
set #finalAddress=#finalAddress + REPLACE(#address,'|',char(13))
if(#lieuDit is not null and #lieuDit != '')
set #finalAddress = #finalAddress + #lieuDit + CHAR(13)
if(#postalCode is not null and #postalCode != '')
set #finalAddress = #finalAddress + #postalCode + CHAR(13)
if(#country is not null and #country != '')
set #finalAddress = #finalAddress + #country + CHAR(13)
end
else
begin
if (#raisonSocial is Null or #raisonSocial = '')
Begin
if(#firstName is not null and #firstName != '')
set #finalAddress = #finalAddress + #firstName + CHAR(13)
if(#familyName is not null and #familyName != '')
set #finalAddress = #finalAddress + #familyName + CHAR(13)
end
else
set #finalAddress = #finalAddress + #raisonSocial + CHAR(13)
if(#jeuneFille is not null and #jeuneFille !='' )
set #finalAddress=CHAR(13) + #finalAddress + ' ' + #jeuneFille + CHAR(13)
if(#saleName is not null and #saleName != '')
set #finalAddress=#finalAddress + #saleName + CHAR(13)
if(#enseigne is not null and #enseigne != '')
set #finalAddress=#finalAddress + #enseigne + CHAR(13)
if(#sigle is not null and #sigle != '')
set #finalAddress=#finalAddress + #sigle + CHAR(13)
if(#address is not null and #address != '')
set #finalAddress=#finalAddress + REPLACE(#address,'|',char(13))
end
Create Table #TempAddress
(
TempAddress_ID int ,
adresse nvarchar(max)
)
INSERT INTO #TempAddress (#TempAddress.adresse) VALUES(#finalAddress)
SELECT #TempAddress.adresse from #TempAddress
DROP TABLE #TempAddress
END
the current result is
aaaaa bbbbb ccccc dddddd
but my expected result is
aaaaa
bbbbb
ccccc
ddddd
Any suggestion or help ?
Thanks

As I explained in my comment, on Windows a newline is CHAR(13)+CHAR(10), but whether that is rendered as a newline depends on how you print it.
in the SELECT statment no line break ,it generates a White space [...] does not work in a simple SELECT
"simple SELECT" does not explain what you're trying to do. Please rember that you are the only one here that can see your screen. You need to be explicit if you want others to understand your problem.
If you mean that the grid view in SQL Server Management Studio doesn't display newlines, you're correct: it doesn't, until you click "Results to Text" button in the toolbar as suggested in the first comment.

Your T-SQL is working fine it is inserting char(13), however depending on your client you need to add a extra char(10).
char(13) + char(10)
It is probably a better idea to do the formatting like this at client side and not in your sql query.

You can also use this syntax:
......note this last single quote after line 2...
| -> press enter and begin a new line with another...
SELECT 'use ' + DB_NAME() + ';' + '
' + 'CREATE USER ' + users.name + ' for ' + users.name + ';'
| -> continued quote in line 3.
This will produce the following output:
use MyDatabase;
CREATE USER User1 for User1;

Related

Retrieve particular value from multiple tables

I have 14 tables of different kinds of employees. I have a C# application which is wired to my SQL Server database and when I type a last name in a textbox it brings back a record with that last name and displays it in a listbox.
However I have managed to do this with only one table. So if I type “Jones” it will bring back and display Jones from one table.
I would like to bring back all the Jones’ from all 14 tables. In other words, when I type a last name, I need the application to show me all records of that last name from all 14 tables.
What would be a reasonable approach to this? It would be a lot easier if I had one table with all employees but I need the seperation. Basically when I click the search button I need the application to go fetch from any of the 14 tables with the given name.
What would be a suitable approach to this?
Define the following stored procedure in your database:
CREATE PROCEDURE GetAll_SP
(
#FirstName VARCHAR(50)
)
AS
BEGIN
(SELECT 1, first_name, last_name FROM UsersTable1 WHERE first_name = #FirstName)
UNION
(SELECT 2, first_name, last_name FROM UsersTable2 WHERE first_name = #FirstName)
UNION
(SELECT 3, first_name, last_name FROM UsersTable3 WHERE first_name = #FirstName)
-- ....
END
GO
or the following one instead of you don't need to have any kind of control over your users location:
CREATE PROCEDURE GetAll_SP
(
#FirstName VARCHAR(50)
)
AS
BEGIN
(SELECT first_name, last_name FROM UsersTable1 WHERE first_name = #FirstName)
UNION ALL
(SELECT first_name, last_name FROM UsersTable2 WHERE first_name = #FirstName)
UNION ALL
(SELECT first_name, last_name FROM UsersTable3 WHERE first_name = #FirstName)
-- ....
END
GO
Then, in your code:
String firstName = "Jones";
using (SqlCommand cmd = new SqlCommand("GetAll_SP", m_Connection))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("#FirstName", SqlDbType.VarChar).Value = firstName;
m_Connection.Open();
cmd.ExecuteNonQuery();
}
You should repeat your Code 14 Times to collect values from all this tables or you submit a SQL Query for all 14 Tables at once with unions or 14 queries in one Statement - depending on your Data Access Technology...
Okay, to get results from all 14 tables you can use UNION ALL Operator, so your SQL would look something like:
(SELECT first_name, last_name FROM table1 WHERE first_name='John')
UNION ALL
(SELECT first_name, last_name FROM table2 WHERE first_name='John')
...
(SELECT first_name, last_name FROM table14 WHERE first_name='John')
You need to have equal amount of fields in every select. However better approach would be if you save all names (and any other shared data) in one table, and connect with Key with all those 14 tables that have different dataset. That way you can prevent such a long queries like this one above (and probably slow queries) and query would look more like this:
SELECT first_name, last_name, user_type, user_id WHERE first_name='John'
And then you can retreive fields from corresponding table as field user_type gives you info in which out of 14 tables to search for other data and user_id gives you data of that user, so second query would look something like this:
SELECT job_position, worksheet, other_data FROM tableN WHERE user_id=...
Simply set #SearchStr and every column in every table will be searched.
drop table #results
go
declare #SearchStr nvarchar(100)
set #SearchStr = 'Donna%' -- use wildcards
CREATE TABLE #Results (ColumnName nvarchar(370), ColumnValue nvarchar(3630))
SET NOCOUNT ON
DECLARE #TableName nvarchar(256), #ColumnName nvarchar(128), #SearchStr2 nvarchar(110)
SET #TableName = ''
SET #SearchStr2 = QUOTENAME('%' + #SearchStr + '%','''')
WHILE #TableName IS NOT NULL
BEGIN
SET #ColumnName = ''
SET #TableName =
(
SELECT MIN(QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME))
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE'
AND QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME) > #TableName
AND OBJECTPROPERTY(
OBJECT_ID(
QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME)
), 'IsMSShipped'
) = 0
)
WHILE (#TableName IS NOT NULL) AND (#ColumnName IS NOT NULL)
BEGIN
SET #ColumnName =
(
SELECT MIN(QUOTENAME(COLUMN_NAME))
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = PARSENAME(#TableName, 2)
AND TABLE_NAME = PARSENAME(#TableName, 1)
AND DATA_TYPE IN ('char', 'varchar', 'nchar', 'nvarchar', 'text')
AND QUOTENAME(COLUMN_NAME) > #ColumnName
)
print cast(#TableName as nvarchar(200)) + ' ' + #ColumnName
IF #ColumnName IS NOT NULL
BEGIN
INSERT INTO #Results
EXEC
(
--'SELECT ''' + #TableName + '.' + #ColumnName + ''', LEFT(' + #ColumnName + ', 3630)
--FROM ' + #TableName + ' (NOLOCK) ' +
--' WHERE ' + #ColumnName + ' LIKE ' + #SearchStr2
'SELECT ''' + #TableName + '.' + #ColumnName + ''', ' + #ColumnName + '
FROM ' + #TableName + ' (NOLOCK) ' +
' WHERE ' + #ColumnName + ' LIKE ' + #SearchStr2
)
END
END
END
SELECT ColumnName, ColumnValue FROM #Results

Joining across several tables - then applying a PIVOT to display the data differently

I am trying to figure a way to execute a seemingly complex join scenario, and am unsure as to how to go about it.
Some Background info:
-I have a 'ProjectCategory' table, which contains a foreign key 'ProjectID' and 'CategoryID' to the Project and Category tables respectively. One project could have as many assigned categories to it as there are existing (up to 10)
-I have a 'Budget' table and a 'Sponsor' Table.
-My 'Project' table is related to my 'Budget' Table in that all Projects have an associated BudgetID
-My 'Budget' Table is related to my 'Sponser' table in that all Budgets have an associated SponsorID.
-'Project' table and 'Sponsor' table are not directly related.
An example of the result set that I am trying to get is firstly:
SponsorName(Field in sponsor table) - ProjectName - Category
___________________________________ ___________ ________
A ABC categoryA
A ABC categoryB
A DEF categoryX
A DEF categoryZ
I would then like to use a PIVOT to show the data like:
SponsorName - ProjectName -categoryA - categoryB -categoryC - categoryD ...
___________ ___________ _________ _________ _________ _________
A ABC X X
A DEF X X
B EFG X X
Where the Xs mark which categories are associated with each project/sponsor combination. The filling in of the Xs is maybe something I will do in the codebehind or using other stored procedures, but this is the basic idea of what I am trying to do.
I am having trouble even figuring out how to write a query to get back the first set before I even implement a pivot to show it as the second set, so I am a bit intimidated by this task. Any help greatly appreciated, and please let me know if you need any more information
Assuming SQL Server, I use a stored procedure for the bulk of Dynamic PIVOTS. (Listed Below)
The source could be a table, #temp or even SQL
Exec [prc-Pivot] '#Temp','Category','max(''X'')[]','SponsorName,ProjectName',null
Returns
SponsorName ProjectName categoryA categoryB categoryD categoryX categoryZ
A ABC X X NULL NULL NULL
A DEF NULL NULL NULL X X
B EFG X NULL X NULL NULL
The Stored Procedure
CREATE PROCEDURE [dbo].[prc-Pivot] (
#Source varchar(1000), -- Any Table or Select Statement
#PvotCol varchar(250), -- Field name or expression ie. Month(Date)
#Summaries varchar(250), -- aggfunction(aggValue)[optionalTitle]
#GroupBy varchar(250), -- Optional additional Group By
#OtherCols varchar(500) ) -- Optional Group By or aggregates
AS
--Exec [prc-Pivot] 'Select Year=Year(TR_Date),* From [Chinrus-Series].[dbo].[DS_Treasury_Rates]','''Q''+DateName(QQ,TR_Date)','avg(TR_Y10)[-Avg]','Year','count(*)[Records],min(TR_Y10)[Min],max(TR_Y10)[Max],Avg(TR_Y10)[Avg]'
Set NoCount On
Set Ansi_Warnings Off
Declare #Vals varchar(max),#SQL varchar(max);
Set #Vals = ''
Set #OtherCols= IsNull(', ' + #OtherCols,'')
Set #Source = case when #Source Like 'Select%' then #Source else 'Select * From '+#Source end
Create Table #TempPvot (Pvot varchar(100))
Insert Into #TempPvot
Exec ('Select Distinct Convert(varchar(100),' + #PvotCol + ') as Pvot FROM (' + #Source + ') A')
Select #Vals = #Vals + ', isnull(' + Replace(Replace(#Summaries,'(','(CASE WHEN ' + #PvotCol + '=''' + Pvot + ''' THEN '),')[', ' END),NULL) As [' + Pvot ) From #TempPvot Order by Pvot
Drop Table #TempPvot
Set #SQL = Replace('Select ' + Isnull(#GroupBy,'') + #OtherCols + #Vals + ' From (' + #Source + ') PvtFinal ' + case when Isnull(#GroupBy,'')<>'' then 'Group By ' + #GroupBy + ' Order by ' + #GroupBy else '' end,'Select , ','Select ')
--Print #SQL
Exec (#SQL)
Set NoCount Off
Set Ansi_Warnings on

Adding conditions in where statement

I have this Linq query:
IQueryable<SPR> query = db.SPRs;
if (!string.IsNullOrEmpty(search.accountNumber))
{
query = query.Where(b => b.CustomerAccountNumber.Contains(search.accountNumber));
}
if (!string.IsNullOrEmpty(search.accountName))
{
query = query.Where(b => b.CustomerNumber.Contains(search.accountName));
}
if (!string.IsNullOrEmpty(search.submittedBy))
{
query = query.Where(b => b.SubmittedBy.Contains(search.submittedBy));
}
if (!string.IsNullOrEmpty(search.smName))
{
query = query.Where(b => b.SMUserName == search.smName);
}
var result = query.ToList();
I am just appending the where clause if conditions are true. The issue is that it is not just adding a And in the generated SQL where clause like I want it to.
Here is the generated SQL if I have the SubmittedBy and SMUserName filled with data.
SELECT
[Extent1].[Id] AS [Id],
[Extent1].[CustomerNumber] AS [CustomerNumber],
[Extent1].[CustomerAccountNumber] AS [CustomerAccountNumber],
[Extent1].[SMUserName] AS [SMUserName],
[Extent1].[SubmittedBy] AS [SubmittedBy],
[Extent1].[Notes] AS [Notes]
FROM
[dbo].[SPRs] AS [Extent1]
WHERE
([Extent1].[SubmittedBy] LIKE #p__linq__0 ESCAPE N'~')
AND (([Extent1].[SMUserName] = #p__linq__1) OR (([Extent1].[SMUserName] IS NULL)
AND (#p__linq__1 IS NULL)))
Not sure how this last line OR (([Extent1].[SMUserName] IS NULL) AND (#p__linq__1 IS NULL))) is getting added which is messing the query up.
Can someone please tell me how I can have just AND in the eventual query when the if conditions are satisfied?
Since you are working with sql server a more performance efficient and sleek way would be to handle the optional parameters inside a stored procedure and make use of Dynamic sql with sp_executesql to benefit from Parameterised Execution Plans.
CREATE PROCEDURE getSPR
#SubmittedBy Varchar(100) = NULL --<--- Use appropriate datatypes
,#CustomerAccountNumber Varchar(100) = NULL
,#CustomerNumber Varchar(100) = NULL
,#SMUserName Varchar(100) = NULL
AS
BEGIN
SET NOCOUNT ON;
Declare #Sql Nvarchar(max);
SET #Sql = N'SELECT [Id]
,[CustomerNumber]
,[CustomerAccountNumber]
,[SMUserName]
,[SubmittedBy]
,[Notes]
FROM [dbo].[SPRs]
WHERE 1 = 1 '
+ CASE WHEN #SubmittedBy IS NOT NULL THEN
N' AND [SubmittedBy] LIKE ''%'' + #SubmittedBy + ''%''' ELSE N' ' END
+ CASE WHEN #CustomerAccountNumber IS NOT NULL THEN
N' AND [CustomerAccountNumber] LIKE ''%'' + #CustomerAccountNumber + ''%''' ELSE N' ' END
+ CASE WHEN #CustomerNumber IS NOT NULL THEN
N' AND [CustomerNumber] LIKE ''%'' + #CustomerNumber + ''%''' ELSE N' ' END
+ CASE WHEN #SMUserName IS NOT NULL THEN
N' AND [SMUserName] = #SMUserName ' ELSE N' ' END
Exec sp_executesql #sql
,N' #SubmittedBy Varchar(100),#CustomerAccountNumber Varchar(100)
,#CustomerNumber Varchar(100), #SMUserName Varchar(100)'
,#SubmittedBy
,#CustomerAccountNumber
,#CustomerNumber
,#SMUserName
END

SQL Server column as a parameter when the column is datetime type

I want to use .net/C# to develop a web application. I have a SQL Server stored procedure and I want to pass dynamic datetime parameter.
When I use exec(#sql) here is the stored procedure with parameter I get :
Error:System.Data.SqlClient.SqlException:
Conversion failed when converting datetime from character string.
I even tried passing some actual values (see below) and also tried debug in SQL Server, it doesn't work.
Can anyone help me out? Thanks a lot in advance.
ALTER PROCEDURE [dbo].[production_tb_WorkshopDailyReports_INSERT]
#intDepartmentID int,
#intReportUserID int,
#intProductionLineID int,
#intBatchID int,
#intVINID int,
#columnname nvarchar(255),
#dtOnlineTime datetime,
#strRemark nvarchar(50),
#intCreateUserID int
AS
BEGIN
declare #column nvarchar(255)
declare #sql nvarchar(1000)
declare #intID int
set #columnname = 'dtOnlineTimeChassis'
set #column = #columnname
set #intDepartmentID = 1
set #intReportUserID = 1
set #intProductionLineID = 1
set #intBatchID = 1
set #intVINID = 1
set #dtOnlineTime = '2016-04-26 10:00:00pm'
set #strRemark = 'remark information'
set #intCreateUserID = 1
set #sql = 'Insert Into production_tb_WorkshopDailyReports (intDepartmentID, intReportUserID, intProductionLineID, intBatchID, intVINID,' + #column + ', strRemark, intCreateUserID) values ' + '(' + cast(#intDepartmentID as nvarchar(20)) + ',' + cast(#intReportUserID as nvarchar(20)) + ',' + cast(#intProductionLineID as nvarchar(20)) + ',' + cast(#intBatchID as nvarchar(20)) + ',' + cast(#intVINID as nvarchar(20)) + ',' + #dtOnlineTime + ',' + #strRemark + ',' + cast(#intCreateUserID as nvarchar(20)) + ')'
(#intReportUserID as nvarchar(20))+','+cast(#intProductionLineID as nvarchar(20))+','+cast(#intBatchID as nvarchar(20))+','+cast(#intVINID as nvarchar(20))+','+''+','+cast(#intCreateUserID as nvarchar(20))+')'
exec(#sql)
End
I guess all this:
set #columnname = 'dtOnlineTimeChassis'
set #column = #columnname
set #intDepartmentID = 1
set #intReportUserID = 1
set #intProductionLineID = 1
set #intBatchID = 1
set #intVINID = 1
set #dtOnlineTime = '2016-04-26 10:00:00pm'
set #strRemark = 'remark information'
set #intCreateUserID = 1
Is just for testing the SP, other time there will be something different in #columnname variable. If this statement is false then I suggest using simple INSERT instead of dynamic SQL.
At first change this:
',' + #dtOnlineTime + ',' + #strRemark + ','
To this:
',''' + CONVERT(nvarchar(50),#dtOnlineTime,120)+''',''' + #strRemark + ''','
I suggest using 120 date style (ODBC canonical yyyy-mm-dd hh:mi:ss(24h), for more info read MSDN) and you must put datetime variable and variable with some remark into ''.
Second remove or comment this part (next string after set #sql = ...):
(#intReportUserID as nvarchar(20))+','+cast(#intProductionLineID as nvarchar(20))+','+cast(#intBatchID as nvarchar(20))+','+cast(#intVINID as nvarchar(20))+','+''+','+cast(#intCreateUserID as nvarchar(20))+')'
After PRINTing your query you will see something like this:
Insert Into production_tb_WorkshopDailyReports (intDepartmentID, intReportUserID, intProductionLineID, intBatchID, intVINID,dtOnlineTimeChassis, strRemark, intCreateUserID) values
(1,1,1,1,1,'2016-04-26 22:00:00','remark information',1)
Try to execute it.
+ ',''' + convert(varchar(26), #dtOnlineTime, 121) + ''',''' + #strRemark + ''',' +
Use CONVERT to convert the date value into a string.
Wrap dates and string values in quotes.

Entity Framework Conditional Left Join and Custom Column Select

I have a stored procedure which results in lots of data. and also want to convert this to EF
unable to figure out how to join to the relavent tables when an attribute is present for the system. and also the column selection is very dynamic in nature,
I could take this sql and execute this directly and get things sorted that way but would miss but the grid in the front end wont be able to handle 600mb of data thrown from the database.
so need paging thought can do this better with EF.
for reference purpose I have the following sql below.
Declare #SQL varchar(max);
Declare #SelectColumns VARCHAR(MAX)
SELECT DISTINCT #SelectColumns= STUFF((SELECT ',''' + [PrimaryDataSource] + ''' Golden'
+ ISNULL(CASE WHEN System1 IS NOT NULL THEN ', System1.' + QUOTENAME([System1]) + ' System1' END, '')
+ ISNULL(CASE WHEN System2 IS NOT NULL THEN ', System2.' + QUOTENAME([System2]) + ' System2' END, '')
+ ISNULL(CASE WHEN [System3] IS NOT NULL THEN ', System3.' + QUOTENAME([System3])+ ' System3' END, '')
+ ISNULL(CASE WHEN System4 IS NOT NULL THEN ', System4.' + QUOTENAME(System4)+ ' System4' END, '')
+ ISNULL(CASE WHEN System5 IS NOT NULL THEN ', System5.' + QUOTENAME(System5)+ ' System5' END, '')
+ ISNULL(CASE WHEN System6 IS NOT NULL THEN ', System6.' + QUOTENAME(System6)+ ' System6' END, '')
FROM [dbo].[TBL_Mapping]
where Attribute =#attributeName
FOR XML PATH(''), TYPE
).value('.', 'VARCHAR(MAX)')
,1,1,'')
SET #SQL = '
SELECT distinct
m.ID MappingID,
m.KeyValueUniqueKey,
m.ValueKeyUniqueKey,
' + #SelectColumns + '
FROM [dbo].[TBL_Mapping] M '
IF CHARINDEX('System1.',#SelectColumns) > 0
BEGIN
SET #SQL = #SQL +
'
LEFT OUTER JOIN dbo.VW_System1_ALL System1 ON
System1.System1ID=M.System1ID '
END
IF CHARINDEX('System2.',#SelectColumns) > 0
BEGIN
SET #SQL = #SQL +
'
LEFT OUTER JOIN dbo.TBL_System2 System2 ON
M.System2ID= System2.System2ID '
END
IF CHARINDEX('System4.',#SelectColumns) > 0
BEGIN
SET #SQL = #SQL + '
LEFT OUTER JOIN DBO.tbl_System4 System4 ON
System4.Key1 = M.KeyValueUniqueKey AND
System4.Value1 = ValueKeyUniqueKey '
END
IF CHARINDEX('System5.',#SelectColumns) > 0
BEGIN
SET #SQL = #SQL + '
LEFT OUTER JOIN DBO.tbl_System5 System5 ON
System5.System5Id = M.System5Id'
END
IF CHARINDEX('System6.',#SelectColumns) > 0
BEGIN
SET #SQL = #SQL + '
LEFT OUTER JOIN dbo.tbl_system6 System6 ON
System6.System6Id = M.System6Id'
END
IF CHARINDEX('System3.',#SelectColumns) > 0
BEGIN
SET #SQL = #SQL + '
LEFT OUTER JOIN [dbo].[TBL_System3] System3 ON
System3.System3Id = M.System3Id'
END
SET #SQL = #SQL + '
WHERE m.version=0 and isActive=1
ORDER by m.ID'
print #SQL
exec (#SQL)
I have looked at the Leftjoin2 extn method but that is not helping much.
What is the best possible action to get this on to EF.
or EF itself is a wrong choise for this sort of problems?
You can do dynamic query generating and then in the end do Skip().Take().
Your model for custom object may look like this:
class MappingData
{
//not sure what the data types are.
int MappingId;
int KeyValueUniqueKey;
int ValueKeyUniqueKey;
string System1;
string System2;
...
string System6;
}
Then in the get method map data,
IQueryable<MappingData> sql = db.TBL_Mapping
.Select(m => new MappingData {
MappingId = ID,
KeyValueUniqueKey = KeyValueUniqueKey,
ValueKeyUniqueKey = ValueKeyUniqueKey,
//leave other columns out
//they will be filled in
//dynamically
})
.Distinct();//get distinct
//--------------------
//REPEAT START
bool HasSystem1 = db.TBL_Mapping.Any(m => m.System1 != null);
//left outer join with System1 if it has it in the TBL_Mapping
if (HasSystem1)
{
sql =
from m in sql
join s1 in db.VW_System1_ALL
on m.System1ID equals s1.System1ID into stemp
from st in stemp.DefaultIfEmpty()
select new { MappingId = st.Id,
KeyValueUniqueKey = st.KeyValueUniqueKey,
ValueKeyUniqueKey = st.ValueKeyUniqueKey,
System1 = st.System1 }; //SystemX column.
}
//REPEAT END
//--------------------
// repeat the above for System2 thru System6
//And in the end do paging.
var result = sql
.Skip(currentPageNumber * numberOfObjectsInPage)
.Take(numberOfObjectsInPage);
This is a bad fit for EF. If all you are only trying to add paging -- add your own paging functionality to the stored proc. You can do this by using ROW_NUMBER OVER what every you are sorting by, then use an an outer query to return the page of data you want, for example...
CREATE PROCEDURE [dbo].[PagedSomething]
#pageSize int,
#pageNum int -- assume pages are 0-based
AS
BEGIN
-- outer query does the paging in its where clause,
-- returning the selected "pages" from the raw results of the inner query
SELECT RawResults.SomethingId
FROM
-- inner query where you make your basic data
(SELECT
s.SomethingId
, ROW_NUMBER() OVER(ORDER BY s.SomethingId) RowID
FROM Somethings s) RawResults
WHERE RowID >= #pageNum * #pageSize + 1
AND RowID < (#pageNum + 1) * #pageSize + 1
END

Categories

Resources