How can I select information from tables from many bases in one result. My idea is to put them in DataTable and show them in DataGrid.
Here is query:
exec sp_msforeachdb 'use ?; IF ''?'' <> ''master'' AND ''?'' <> ''model'' AND ''?'' <> ''msdb'' AND ''?'' <> ''tempdb'' AND ''?'' <> ''addresses'' AND ''?'' <> ''ikamebeldizain'' AND ''?'' <> ''new2'' AND ''?'' <> ''sample'' AND ''?'' <> ''sitedatabase'' AND ''?'' <> ''StudentsTeachersTest'' AND ''?'' <> ''MicroinvestDatabasesCatalog'' select * from dbo.system;'
When I try with this query in dataGrid i have just one line (first result).
To append multiple result sets in SQL, you need to have identical results structure (ie: the same columns are returned in the same order) and to tell SQL to bolt them together with a union all (Using just union will remove duplicate rows). To do this across different databases, assuming they are on the same server you can just reference the database in your query:
select *
from Database1.dbo.system
union all
select *
from Database2.dbo.system
If you are trying to do this across a dynamic set of databases, you will only be able to do this with dynamic SQL, which will output the above statement based on a list of databases provided earlier in the script, which you can then execute.
I make a solution:
DECLARE #db_id AS int
DECLARE #db_name AS sysname
CREATE TABLE ##CompatibleDatabases
(
Name sysname,
CompanyName nvarchar(255),
ProductID smallint,
[Version] nvarchar(20),
[Code] int
)
SET ROWCOUNT 0
SELECT dbid AS [ID], Name AS [Name] INTO #AllDatabases FROM master..sysdatabases
SET ROWCOUNT 1
SELECT #db_id = [ID] FROM #AllDatabases
WHILE ##rowcount <> 0
BEGIN
SET ROWCOUNT 0
BEGIN TRY
SET #db_name = db_name(#db_id)
EXEC ('IF (
EXISTS ( SELECT * FROM [' + #db_name + '].INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME=''System'') AND
EXISTS ( SELECT * FROM [' + #db_name + '].INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME=''System'' AND COLUMN_NAME=''ProductID'' ) AND
EXISTS ( SELECT * FROM [' + #db_name + '].INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME=''System'' AND COLUMN_NAME=''Version'' )
)
BEGIN
DECLARE #insertStatement nvarchar(500)
IF (EXISTS ( SELECT * FROM [' + #db_name + '].INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME=''System'' AND COLUMN_NAME=''CompanyName'' ) )
SET #insertStatement=''INSERT INTO ##CompatibleDatabases SELECT ''''' + #db_name + ''''' AS Name, CompanyName, ProductID, [Version], [Code] FROM [' + #db_name + '].dbo.System''
ELSE
SET #insertStatement=''INSERT INTO ##CompatibleDatabases SELECT ''''' + #db_name + ''''' AS Name,''''' + #db_name + ''''' AS CompanyName, ProductID, [Version], NULL AS Code FROM [' + #db_name + '].dbo.System''
EXEC(#insertStatement)
END')
END TRY
BEGIN CATCH
END CATCH
DELETE #AllDatabases WHERE [ID] = #db_id
SET ROWCOUNT 1
SELECT #db_id = [ID] FROM #AllDatabases
END
SET ROWCOUNT 0
select * from ##CompatibleDatabases
DROP TABLE #AllDatabases
DROP TABLE ##CompatibleDatabases
Related
I want to be able to merge duplicate records in 1 table, and update all child entities whose FK was the originalRecordId to the destinyRecordId. This could be done manually, but it would be a daunting task as the parent table could have 40+ relationships, and this has to be done in multiple parent tables. I am using EF 6 with code first.
I have been able to get all navigation properties using the following (credit goes to #zev-spits in this question):
public List<PropertyInfo> GetNavigationProperties(T entity)
{
var t = entity.GetType();
var elementType = ((IObjectContextAdapter)context).ObjectContext.CreateObjectSet<T>().EntitySet.ElementType;
return elementType.NavigationProperties.Select(np => entityType.GetProperty(np.Name)).ToList();
}
I have no idea where to go from here.
Is there a way to programmatically do this task? I would like to pass in a originalRecord and a destinyRecord, obtain all related entities of the originalRecord and update the FK with the destinyRecordId.
I finally took #mjwillis suggestion and resolved this directly in SQL Server with the following:
DECLARE #sql nvarchar (255);
DECLARE #refTableName varchar(255) = 'Opportunities'
DECLARE #refColName varchar(255) = 'Id'
DECLARE #refValue uniqueidentifier = '2a8a0f61-fe5c-4dd7-8e6d-00c8340333c4'
Deleted User Key
DECLARE #refNewValue uniqueidentifier = '99999999-9999-9999-9999-999999999999'
DECLARE RelatedTableCursor CURSOR
LOCAL STATIC READ_ONLY FORWARD_ONLY
FOR
SELECT
result.TableName,
result.ColumnName
FROM
(
SELECT
OBJECT_NAME(f.parent_object_id) AS TableName,
COL_NAME(fc.parent_object_id,fc.parent_column_id) AS ColumnName,
SCHEMA_NAME(o.SCHEMA_ID) ReferenceSchemaName,
OBJECT_NAME (f.referenced_object_id) AS ReferenceTableName,
COL_NAME(fc.referenced_object_id,fc.referenced_column_id) AS ReferenceColumnName
FROM sys.foreign_keys AS f
INNER JOIN sys.foreign_key_columns AS fc ON f.OBJECT_ID = fc.constraint_object_id
INNER JOIN sys.objects AS o ON o.OBJECT_ID = fc.referenced_object_id
) result
WHERE
ReferenceTableName = #refTableName AND ReferenceColumnName = #refColName
OPEN RelatedTableCursor
WHILE 1 = 1
BEGIN
DECLARE #tableName varchar(255)
DECLARE #columnName varchar(255)
FETCH NEXT FROM RelatedTableCursor into #tableName, #columnName
IF ##fetch_status <> 0
BEGIN
BREAK
END
DECLARE #sql nvarchar (255);
SET #sql =
N'UPDATE [' + #tableName + '] ' +
'SET ' +
' [' + #columnName + '] = ''' + CONVERT(NVARCHAR(255), #refNewValue) + ''' ' +
'WHERE [' + #columnName + '] = ''' + CONVERT(NVARCHAR(255), #refValue) + ''' ';
-- Prevent some error (Duplicate record) that occurs when replace data in mapping table.
BEGIN TRY
-- exec sp_executesql #sql;
select #sql
END TRY
BEGIN CATCH
END CATCH
END
CLOSE RelatedTableCursor
DEALLOCATE RelatedTableCursor
SET #sql = 'DELETE FROM [' + #refTableName + '] WHERE ' + #refColName + ' = ''' + CONVERT(NVARCHAR(255), #refValue) + '''';
EXEC sp_executesql #sql;
I'm trying to write a stored procedure to search for a string in all tables of a SQL Server database. I was able to find a good stored procedure for this purpose
However I don't want to just put the #Tablenames manually, I want it to go to a table called Enums_Tables that has an ID, and use that ID as #Tablenames.
What I have being thinking to solve this:
I could write another stored procedure to select all Id's from Enums_Tables and execute the first stored procedure, like in here
I could also pass the parameter in C#, since I'm going to use it as a search textbox. But the ideal would be making a single stored procedure.
Could you please help me with this?
EDIT
Thanks to GPW I have been able to solve this problem. I also encountered problems with the collation, but I also solve it. Below is the final stored procedure.
USE [DynaForms]
GO
/****** Object: stored procedure [dbo].[SP_SearchTables] Script Date: 09/11/2017 14:59:15 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[SP_SearchTables]
--#Tablenames VARCHAR(500)
#SearchStr NVARCHAR(60)
,#GenerateSQLOnly Bit = 0
,#SchemaNames VARCHAR(500) ='%'
AS
/*
Parameters and usage
#Tablenames -- Provide a single table name or multiple table name with comma seperated.
If left blank , it will check for all the tables in the database
Provide wild card tables names with comma seperated
EX :'%tbl%,Dim%' -- This will search the table having names comtains "tbl" and starts with "Dim"
#SearchStr -- Provide the search string. Use the '%' to coin the search. Also can provide multiple search with comma seperated
EX : X%--- will give data staring with X
%X--- will give data ending with X
%X%--- will give data containig X
%X%,Y%--- will give data containig X or starting with Y
%X%,%,,% -- Use a double comma to search comma in the data
#GenerateSQLOnly -- Provide 1 if you only want to generate the SQL statements without seraching the database.
By default it is 0 and it will search.
##SchemaNames -- Provide a single Schema name or multiple Schema name with comma seperated.
If left blank , it will check for all the tables in the database
Provide wild card Schema names with comma seperated
EX :'%dbo%,Sales%' -- This will search the Schema having names comtains "dbo" and starts with "Sales"
Samples :
1. To search data in a table
EXEC SP_SearchTables #Tablenames = 'T1'
,#SearchStr = '%TEST%'
The above sample searches in table T1 with string containing TEST.
2. To search in a multiple table
EXEC SP_SearchTables #Tablenames = 'T2'
,#SearchStr = '%TEST%'
The above sample searches in tables T1 & T2 with string containing TEST.
3. To search in a all table
EXEC SP_SearchTables #Tablenames = '%'
,#SearchStr = '%TEST%'
The above sample searches in all table with string containing TEST.
4. Generate the SQL for the Select statements
EXEC SP_SearchTables #Tablenames = 'T1'
,#SearchStr = '%TEST%'
,#GenerateSQLOnly = 1
5. To Search in tables with specfic name
EXEC SP_SearchTables #Tablenames = '%T1%'
,#SearchStr = '%TEST%'
,#GenerateSQLOnly = 0
6. To Search in multiple tables with specfic names
EXEC SP_SearchTables #Tablenames = '%T1%,Dim%'
,#SearchStr = '%TEST%'
,#GenerateSQLOnly = 0
7. To specify multiple search strings
EXEC SP_SearchTables #Tablenames = '%T1%,Dim%'
,#SearchStr = '%TEST%,TEST1%,%TEST2'
,#GenerateSQLOnly = 0
8. To search comma itself in the tables use double comma ",,"
EXEC SP_SearchTables #Tablenames = '%T1%,Dim%'
,#SearchStr = '%,,%'
,#GenerateSQLOnly = 0
EXEC SP_SearchTables #Tablenames = '%T1%,Dim%'
,#SearchStr = '%with,,comma%'
,#GenerateSQLOnly = 0
9. To Search by SchemaName
EXEC SP_SearchTables #Tablenames = '%T1%,Dim%'
,#SearchStr = '%,,%'
,#GenerateSQLOnly = 0
,#SchemaNames = '%dbo%,Sales%'
*/
SET NOCOUNT ON
DECLARE #MatchFound BIT
SELECT #MatchFound = 0
DECLARE #CheckTableNames Table
(
Schemaname sysname
,Tablename sysname
)
DECLARE #SearchStringTbl TABLE
(
SearchString VARCHAR(500)
)
DECLARE #SQLTbl TABLE
(
Tablename SYSNAME
,WHEREClause VARCHAR(MAX)
,SQLStatement VARCHAR(MAX)
,Execstatus BIT
)
DECLARE #SQL VARCHAR(MAX)
DECLARE #TableParamSQL VARCHAR(MAX)
DECLARE #SchemaParamSQL VARCHAR(MAX)
DECLARE #TblSQL VARCHAR(MAX)
DECLARE #tmpTblname sysname
DECLARE #ErrMsg VARCHAR(100)
/*
IF LTRIM(RTRIM(#Tablenames)) IN ('' ,'%')
BEGIN
INSERT INTO #CheckTableNames
SELECT Name
FROM sys.tables
END
ELSE
BEGIN
IF CHARINDEX(',',#Tablenames) > 0
SELECT #SQL = 'SELECT ''' + REPLACE(#Tablenames,',','''as TblName UNION SELECT ''') + ''''
ELSE
SELECT #SQL = 'SELECT ''' + #Tablenames + ''' as TblName '
SELECT #TblSQL = 'SELECT T.NAME
FROM SYS.TABLES T
JOIN (' + #SQL + ') tblsrc
ON T.name LIKE tblsrc.tblname '
INSERT INTO #CheckTableNames
EXEC(#TblSQL)
END
*/
--IF LTRIM(RTRIM(#Tablenames)) = ''
--BEGIN
-- SELECT #Tablenames = '%'
--END
IF LTRIM(RTRIM(#SchemaNames)) =''
BEGIN
SELECT #SchemaNames = '%'
END
--IF CHARINDEX(',',#Tablenames) > 0
-- SELECT #TableParamSQL = 'SELECT ''' + REPLACE(#Tablenames,',','''as TblName UNION SELECT ''') + ''''
--ELSE
-- SELECT #TableParamSQL = 'SELECT ''' + #Tablenames + ''' as TblName '
IF CHARINDEX(',',#SchemaNames) > 0
SELECT #SchemaParamSQL = 'SELECT ''' + REPLACE(#SchemaNames,',','''as SchemaName UNION SELECT ''') + ''''
ELSE
SELECT #SchemaParamSQL = 'SELECT ''' + #SchemaNames + ''' as SchemaName '
SELECT #TblSQL = 'SELECT SCh.NAME,T.NAME
FROM SYS.TABLES T
JOIN SYS.SCHEMAS SCh
ON SCh.SCHEMA_ID = T.SCHEMA_ID
INNER JOIN [DynaForms].[dbo].[Enums_Tables] et on
(et.Id = T.NAME COLLATE Latin1_General_CI_AS)'
INSERT INTO #CheckTableNames
(Schemaname,Tablename)
EXEC(#TblSQL)
IF NOT EXISTS(SELECT 1 FROM #CheckTableNames)
BEGIN
SELECT #ErrMsg = 'No tables are found in this database ' + DB_NAME() + ' for the specified filter'
PRINT #ErrMsg
RETURN
END
IF LTRIM(RTRIM(#SearchStr)) =''
BEGIN
SELECT #ErrMsg = 'Please specify the search string in #SearchStr Parameter'
PRINT #ErrMsg
RETURN
END
ELSE
BEGIN
SELECT #SearchStr = REPLACE(#SearchStr,',,,',',#DOUBLECOMMA#')
SELECT #SearchStr = REPLACE(#SearchStr,',,','#DOUBLECOMMA#')
SELECT #SearchStr = REPLACE(#SearchStr,'''','''''')
SELECT #SQL = 'SELECT ''' + REPLACE(#SearchStr,',','''as SearchString UNION SELECT ''') + ''''
INSERT INTO #SearchStringTbl
(SearchString)
EXEC(#SQL)
UPDATE #SearchStringTbl
SET SearchString = REPLACE(SearchString ,'#DOUBLECOMMA#',',')
END
INSERT INTO #SQLTbl
( Tablename,WHEREClause)
SELECT QUOTENAME(SCh.name) + '.' + QUOTENAME(ST.NAME),
(
SELECT '[' + SC.Name + ']' + ' LIKE ''' + REPLACE(SearchSTR.SearchString,'''','''''') + ''' OR ' + CHAR(10)
FROM SYS.columns SC
JOIN SYS.types STy
ON STy.system_type_id = SC.system_type_id
AND STy.user_type_id =SC.user_type_id
CROSS JOIN #SearchStringTbl SearchSTR
WHERE STY.name in ('varchar','char','nvarchar','nchar','text')
AND SC.object_id = ST.object_id
ORDER BY SC.name
FOR XML PATH('')
)
FROM SYS.tables ST
JOIN #CheckTableNames chktbls
ON chktbls.Tablename = ST.name
JOIN SYS.schemas SCh
ON ST.schema_id = SCh.schema_id
AND Sch.name = chktbls.Schemaname
WHERE ST.name <> 'SearchTMP'
GROUP BY ST.object_id, QUOTENAME(SCh.name) + '.' + QUOTENAME(ST.NAME) ;
UPDATE #SQLTbl
SET SQLStatement = 'SELECT * INTO SearchTMP FROM ' + Tablename + ' WHERE ' + substring(WHEREClause,1,len(WHEREClause)-5)
DELETE FROM #SQLTbl
WHERE WHEREClause IS NULL
WHILE EXISTS (SELECT 1 FROM #SQLTbl WHERE ISNULL(Execstatus ,0) = 0)
BEGIN
SELECT TOP 1 #tmpTblname = Tablename , #SQL = SQLStatement
FROM #SQLTbl
WHERE ISNULL(Execstatus ,0) = 0
IF #GenerateSQLOnly = 0
BEGIN
IF OBJECT_ID('SearchTMP','U') IS NOT NULL
DROP TABLE SearchTMP
EXEC (#SQL)
IF EXISTS(SELECT 1 FROM SearchTMP)
BEGIN
SELECT Tablename=#tmpTblname,* FROM SearchTMP
SELECT #MatchFound = 1
END
END
ELSE
BEGIN
PRINT REPLICATE('-',100)
PRINT #tmpTblname
PRINT REPLICATE('-',100)
PRINT replace(#SQL,'INTO SearchTMP','')
END
UPDATE #SQLTbl
SET Execstatus = 1
WHERE Tablename = #tmpTblname
END
IF #MatchFound = 0
BEGIN
SELECT #ErrMsg = 'No Matches are found in this database ' + DB_NAME() + ' for the specified filter'
PRINT #ErrMsg
RETURN
END
SET NOCOUNT OFF
Sorry in advance with the weird formatting, I can't put it more readable. I hope it helps someone. I also want to give credit to the onwer of the original stored procedure in here.
That stored procedure you linked to already has logic to check the table names in the database and populates a table variable with a list of tables to check. Just change this logic to Select from your ENUM_TABLES table instead. This could be based on an input parameter of #Id if you like...
In simple terms:
Remove the parameter #TableNames from the stored procedure
(optionally) replace the parameter with #ENUM_TABLE_ID or something
Change code in SP that looks like this:
IF LTRIM(RTRIM(#Tablenames)) = ''
/* Removed a load of lines looking at #TableNames...... */
....
SELECT #TblSQL = 'SELECT SCh.NAME,T.NAME
FROM SYS.TABLES T
JOIN SYS.SCHEMAS SCh
ON SCh.SCHEMA_ID = T.SCHEMA_ID
JOIN (' + #TableParamSQL + ') tblsrc
ON T.name LIKE tblsrc.tblname
JOIN (' + #SchemaParamSQL + ') schemasrc
ON SCh.name LIKE schemasrc.SchemaName
With something more like this:
SELECT #TblSQL = 'SELECT SCh.NAME,T.NAME
FROM SYS.TABLES T
JOIN SYS.SCHEMAS SCh
ON SCh.SCHEMA_ID = T.SCHEMA_ID
INNER JOIN ENUM_TABLES et on
(et.TABLENAME=T.NAME)
and (et.Id='+#ENUM_TABLE_ID+')'
And then I think it'll do what you want (lookup the list of tables from another table, based on an ID passed into the stored procedure)
(apologies for the slightly weird formatting of the SQL above; for some reason the SO markdown processor really didn't like that stuff, but it is hopefully readable. If anyone wants to try to edit this to improve it, be my guest.)
I want to PassColumn name as a paremeter in My SP
And if my tat column exists in first table (Batch_Master), want to fatch value from that column,
And if that column exists in my second table (GTIN_Master), want to fetch value from tat table column,
Each table have columns like..
Batch_Master (Batch_M_id, GTIN(primary key),....etc
GTIN_Master (GTIN (foreign key),..etc)
I have Batch_M_id, and column name as a parameter..
Note: column Name having random datatype, some time int or some time datetime etc
I Try followin SP
CREATE PROCEDURE dbo.StoredProcedure2
#columnName varchar(50),
#batchmId int
AS
if exists(select * from sys.columns
where Name = N'columnName' and Object_ID = Object_ID(NBatch_Master'))
begin
select #columnName from Batch_Master
end
else
begin
select #columnName
from GTIN_Master inner join Batch_Master
on GTIN_Master.GTIN = Batch_Master.GTIN
where Batch_M_id =#batchmId
end
RETURN
What you need when you do not know exactly the query structure you are going to execute, then you have to create a dynamic query, which is actually a form of templating queries.
CREATE PROCEDURE dbo.StoredProcedure2
#columnName varchar(50),
#batchmId int
AS
DECLARE #SQL1 AS VARCHAR(MAX)
DECLARE #SQL2 AS VARCHAR(MAX)
SET #SQL1 = 'select ' + #columnName + ' from Batch_Master'
SET #SQL1 = 'select ' + #columnName + '
from GTIN_Master inner join Batch_Master
on GTIN_Master.GTIN = Batch_Master.GTIN
where Batch_M_id =' + CONVERT(VARCHAR,#batchmId)
IF EXISTS(SELECT * FROM sys.columns WHERE Name = #columnName and Object_ID = Object_ID(N'Batch_Master'))
BEGIN
EXEC (#SQL1)
END
ELSE
BEGIN
EXEC (#SQL2)
END
The above will do what you want but is prone to errors. For instance what if the column passed in the parameter does not exist in the tables used in the second query? You should probably need to check for existence in the second case too.
DECLARE #SQL VARCHAR(MAX)
SET #SQL = 'select ' + #columnName + ' from Batch_Master'
EXECUTE(#SQL)
This SQL:
set nocount on
go
create table One (id int, name varchar(25))
go
create table Two (id int, value varchar(25))
go
insert into One values (1, 'Table One')
insert into Two values (1, 'Table Two')
go
create procedure sp_test_colname
#colname sysname
as
declare #sql nvarchar(2048)
if exists (select 1 from sys.columns where name = #colname and object_id = object_id(N'One'))
set #sql = 'select [' + #colname + '] from [One]'
else
if exists (select 1 from sys.columns where name = #colname and object_id = object_id(N'Two'))
set #sql = 'select [' + #colname + '] from [Two]'
if #sql <> ''
exec sp_executesql #sql
go
exec sp_test_colname 'name'
exec sp_test_colname 'value'
exec sp_test_colname 'somethingelse'
go
drop procedure sp_test_colname
drop table One, Two
Returns the following:
name
-------------------------
Table One
value
-------------------------
Table Two
I am trying to marge two rows and making single column. same data of row has to marge, and different data has to append to same row,
example:
I got result from joining three tables like this
uid name diseaseid intensity
1 xxxx 1 high
1 xxxx 2 low
Now I need final result like this
uid name diseaseid1 intensity diseaseid2 intensity
1 xxxx 1 high 2 low
Is there any option to make like this in Sql please help me out.
Thanks in advance.
Try this
CREATE TABLE #Table1
([uid] int, [name] varchar(4), [diseaseid] int, [intensity] varchar(4))
;
INSERT INTO #Table1
([uid], [name], [diseaseid], [intensity])
VALUES
(1, 'xxxx', 1, 'high'),
(1, 'xxxx', 2, 'low')
;
SELECT MAX([uid]) AS [uid]
,MAX([name]) AS [name]
,MAX([diseaseid1]) AS [diseaseid1]
,MAX([intensity1]) AS [intensity1]
,MAX([diseaseid2]) AS [diseaseid2]
,MAX([intensity2]) [intensity2]
FROM
(
SELECT [uid], [name]
, CASE WHEN rn=2 THEN NULL ELSE [diseaseid] END AS [diseaseid1]
, CASE WHEN rn=2 THEN NULL ELSE [intensity] END AS [intensity1]
, CASE WHEN rn=1 THEN NULL ELSE [diseaseid] END AS [diseaseid2]
, CASE WHEN rn=1 THEN NULL ELSE [intensity] END AS [intensity2]
FROM
(
SELECT [uid], [name], [diseaseid], [intensity],
ROW_NUMBER() OVER(PARTITION BY [uid] ORDER BY Name) AS rn
FROM #Table1
) T
) T
GROUP BY [uid], [name]
DROP TABLE #Table1
EIDT
Try this for dynamic columns
CREATE TABLE #Table1
([uid] int, [name] varchar(10), [diseaseid] int, [intensity] varchar(10))
INSERT INTO #Table1
([uid], [name], [diseaseid], [intensity])
VALUES
(1, 'xxxx', 1, 'high'),
(1, 'xxxx', 2, 'low'),
(1, 'xxxx', 3, 'medium')
DECLARE #MaxRows INT
DECLARE #CurrentRow INT
DECLARE #Query VARCHAR(MAX)
SET #CurrentRow = 1
SELECT #MaxRows = MAX(cnt) FROM (
SELECT COUNT(name) cnt FROM #Table1 GROUP BY [uid]
) AS T
SET #Query = '
SELECT MAX([uid]) AS [uid]
,MAX([name]) AS [name]'
WHILE #CurrentRow <= #MaxRows
BEGIN
SET #Query= #Query + '
,MAX(diseaseid'+CONVERT(VARCHAR(10),#CurrentRow)+') AS diseaseid'+CONVERT(VARCHAR(10),#CurrentRow)+''
SET #Query= #Query + '
,MAX(intensity'+CONVERT(VARCHAR(10),#CurrentRow)+') AS intensity'+CONVERT(VARCHAR(10),#CurrentRow)+''
SET #CurrentRow = #CurrentRow + 1
END
SET #Query= #Query + ' FROM
(
SELECT [uid], [name]'
SET #CurrentRow = 1
WHILE #CurrentRow <= #MaxRows
BEGIN
SET #Query= #Query + '
, CASE WHEN rn='+CONVERT(VARCHAR(10),#CurrentRow)+' THEN [diseaseid] ELSE NULL END AS diseaseid'+CONVERT(VARCHAR(10),#CurrentRow)+''
SET #Query= #Query + '
, CASE WHEN rn='+CONVERT(VARCHAR(10),#CurrentRow)+' THEN [intensity] ELSE NULL END AS intensity'+CONVERT(VARCHAR(10),#CurrentRow)+''
SET #CurrentRow = #CurrentRow + 1
END
SET #Query= #Query + ' FROM
(
SELECT [uid], [name], [diseaseid], [intensity],
ROW_NUMBER() OVER(PARTITION BY [uid] ORDER BY Name) AS rn
FROM #Table1
) T2
) T3
GROUP BY [uid], [name]'
PRINT (#Query)
EXEC (#Query)
DROP TABLE #Table1
You can use a query like this one
select *,(select col1,col2 from table2 where uid=t.uid) from table1 t
i think u're doing your joins wrong...
It looks like u're doing a Union of u're only grouping by UID, name.
Without knowing the join, the best advice i can give u is to try experiment with Right and Left outer join.
I think this should do what u want.
Try joining the table u get from the diseaseid twice, once with join and the second time with right outer join, this way u should get the diseaseid2 without problems.
I'm trying to inner join asp_Users & asp_Membership tables so that i can retrieve the CreateDate field from asp_Membership in a BoundField of a GridView i have.
I have tried the following which seems to make sense to me but it keeps throwing the error:
A field or property with the name 'CreateDate' was not found on the selected data source.
ALTER PROCEDURE [dbo].[stp_Customer_Fetch_Paged]
(
#sortExpression nvarchar(100),
#startRowIndex int,
#maximumRows int
)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Make sure a #sortExpression is specified
IF LEN(#sortExpression) = 0
SET #sortExpression = 'aspnet_Users.UserId'
-- Issue query
DECLARE #sql nvarchar(4000)
SET #sql = '
SELECT userid, username, first_name, last_name, town,postcode, rowrank FROM (
SELECT aspnet_Users.UserId, aspnet_Membership.UserId as MembershipID, aspnet_Membership.CreateDate, aspnet_Users.UserName, Customer.title, Customer.first_name, Customer.last_name, Customer.telephone,
Customer.dpa_consent, Customer.billing_address_id, Address.friendly_name, Address.address_line_1, Address.address_line_2,
Address.address_line_3, Address.town, Address.postcode, ROW_NUMBER() OVER (ORDER BY ' + #sortExpression + ' ) AS RowRank
FROM Customer INNER JOIN
aspnet_Users ON Customer.userid = aspnet_Users.UserId
INNER JOIN
Address ON Customer.billing_address_id = Address.address_id AND Customer.userid = Address.user_id
INNER JOIN
aspnet_Membership ON
aspnet_Membership.UserId = aspnet_Users.UserId
) AS ProductsWithRowNumbers
WHERE RowRank > ' + CONVERT(nvarchar(10), #startRowIndex) +
' AND RowRank <= (' + CONVERT(nvarchar(10), #startRowIndex)+ ' + ' +
CONVERT(nvarchar(10), #maximumRows) + ')'
print #sql
-- Execute the SQL query
EXEC sp_executesql #sql
END
Despite me selecting it in the stored procedure. I'm very new to SQL so it might be something simple i've overlooked? Any help is greatly appreciated.
Thank you.
Your outermost SELECT clause doesn't include that column:
SELECT userid, username, first_name, last_name, town,postcode, rowrank
needs to be
SELECT userid, username, first_name, last_name, town,postcode, CreateDate, rowrank