Am using NLog 4.2.2 and asp net 5. All my NLog configurations are on code. I need to deploy my project and want to make sure the appropriate SqlServer Table and StoredProcedure are created. I'm using the DatabaseTarget.Install() method like this:
dbTarget.InstallDdlCommands.Clear();
dbTarget.InstallDdlCommands.Add(new DatabaseCommandInfo
{
ConnectionString = connectionString,
CommandType = System.Data.CommandType.Text,
IgnoreFailures = false,
Text = $#"CREATE TABLE [dbo].[{TableName}] (
[ID] [int] IDENTITY(1,1) NOT NULL,
[MachineName] [nvarchar](200) NULL,
[SiteName] [nvarchar](200) NOT NULL,
[Logged] [datetime] NOT NULL,
[Level] [varchar](5) NOT NULL,
[UserName] [nvarchar](200) NULL,
[Message] [nvarchar](max) NOT NULL,
[Logger] [nvarchar](300) NULL,
[Properties] [nvarchar](max) NULL,
[ServerName] [nvarchar](200) NULL,
[Port] [nvarchar](100) NULL,
[Url] [nvarchar](2000) NULL,
[Https] [bit] NULL,
[ServerAddress] [nvarchar](100) NULL,
[RemoteAddress] [nvarchar](100) NULL,
[Callsite] [nvarchar](300) NULL,
[Exception] [nvarchar](max) NULL,
CONSTRAINT [PK_dbo.Log] 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];"
});
dbTarget.InstallDdlCommands.Add(new DatabaseCommandInfo
{
ConnectionString = connectionString,
CommandType = System.Data.CommandType.Text,
IgnoreFailures = false,
Text = $#"CREATE PROCEDURE [dbo].[{ProcName}] (
#machineName nvarchar(200),
#siteName nvarchar(200),
#logged datetime,
#level varchar(5),
#userName nvarchar(200),
#message nvarchar(max),
#logger nvarchar(300),
#properties nvarchar(max),
#serverName nvarchar(200),
#port nvarchar(100),
#url nvarchar(2000),
#https bit,
#serverAddress nvarchar(100),
#remoteAddress nvarchar(100),
#callSite nvarchar(300),
#exception nvarchar(max)
) AS
BEGIN
INSERT INTO [dbo].[{TableName}] (
[MachineName],
[SiteName],
[Logged],
[Level],
[UserName],
[Message],
[Logger],
[Properties],
[ServerName],
[Port],
[Url],
[Https],
[ServerAddress],
[RemoteAddress],
[CallSite],
[Exception]
) VALUES (
#machineName,
#siteName,
#logged,
#level,
#userName,
#message,
#logger,
#properties,
#serverName,
#port,
#url,
#https,
#serverAddress,
#remoteAddress,
#callSite,
#exception
);
END"
});
using (var context = new InstallationContext())
{
if (dbTarget.IsInstalled(context) != true)
dbTarget.Install(context);
}
But when I run my code I get this exception
I have seen this question and I think I'm doing what they say but still doesn't work for me. I know the rest of the code to be good as DB logging works just fine once I manually create the Table and StoredProcedure
After a while I opened an issue in github and I got the answer from there. Turns out that, as of now, when you programmatically create a configuration file (as I was) you have to manually call configuration.Install(installationContext) in order for the different Targets to be actually initialized, as a side effect it will also run the Install method of the Targets that have it. So all the above code needs to do is
// Target configuration as is (except for the installation part)
config.AddTarget(dbTarget);
using(var context = new InstallationContext())
{
config.Install(context);
}
if there are more than one target though I think config.Install(context) should be called after all target (or at least most of them) have being added as it initializes all registered targets and runs Install on them.
Related
I have a SQL Server table with 10 columns:
CREATE TABLE [bank].[CommonPostingsFromBankFiles]
(
[Id] [INT] IDENTITY(1,1) NOT NULL,
[BankRegistrationNumber] [INT] NOT NULL,
[BankAccountNumber] [BIGINT] NOT NULL,
[BankName] [NVARCHAR](50) NULL,
[BankAccount] [NVARCHAR](50) NULL,
[PostingAmount] [DECIMAL](18, 2) NOT NULL,
[PostingDate] [DATE] NOT NULL,
[Primo] [CHAR](1) NULL,
[PostingText] [NVARCHAR](100) NULL,
[HideThisRecord] [BIT] NULL,
CONSTRAINT [PK_CommonPostingsFromBankFiles]
PRIMARY KEY CLUSTERED ([BankRegistrationNumber] ASC,
[BankAccountNumber] ASC,
[PostingAmount] ASC,
[PostingDate] ASC)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
)
and a stored procedure that takes 7 parameters:
ALTER PROCEDURE [bank].[spInsertCommonPostings]
(#BankRegistrationNumber INT,
#BankAccountNumber BIGINT,
#BankName NVARCHAR(50),
#PostingAmount DECIMAL(18, 2),
#PostingDate DATE,
#Primo CHAR(1),
#PostingText NVARCHAR(100))
AS
BEGIN
IF NOT EXISTS (SELECT *
FROM bank.CommonPostingsFromBankFiles
WHERE BankRegistrationNumber = #BankRegistrationNumber
AND BankAccountNumber = #BankAccountNumber
AND BankName = #BankName
AND PostingAmount = #PostingAmount
AND PostingDate = #PostingDate)
INSERT INTO bank.CommonPostingsFromBankFiles (BankRegistrationNumber, BankAccountNumber,
BankName, PostingAmount,
PostingDate, Primo, PostingText)
VALUES (#BankRegistrationNumber, #BankAccountNumber,
#BankName, #PostingAmount,
#PostingDate, #Primo, #PostingText);
END;
What I want is to use Dapper to write a List<Postings> to the table using the stored procedure.
I have searched and searched but found no example that helped me.
If I do a
connection.Execute(sql: "spMyStoredProc", MyList, commandType: CommandType.StoredProcedure);
I get an error
Procedure or function spMyStoredProc has too many arguments specified
If I replace the name of the stored procedure with the sql from the stored procedure and set CommandType to Text it works as expected.
Could anybody please post me an example showing how to insert my list using my stored procedure.
Thanks,
Steffen
Simplistically, and assuming the names of the properties in your c# object are identical to the names of the parameters in your stored proc, you could:
MyList.ForEach(x => connection.Execute(sql: "spMyStoredProc", x, commandType: CommandType.StoredProcedure));
If the param/props aren't aligned it may be simplest to provide an anonymous type populated with values from x that covers them
MyList.ForEach(x => connection.Execute(
sql: "spMyStoredProc",
new { BankRegistrationNumber = x.BankRN, ... },
commandType: CommandType.StoredProcedure
));
For a list of 10 objects it will invoke the proc 10 times; it's not the fastest way to do it, but I don't think you've stated any particular goals re performance etc.
I have a big table in my SQL Server database as follows:
CREATE TABLE [dbo].[Servers](
[ServerId] [uniqueidentifier] NOT NULL,
[ServerName] [nvarchar](50) NULL,
[HostName] [nvarchar](50) NULL,
[OperatingSystem] [nvarchar](50) NULL,
[OSVersion] [nvarchar](50) NULL,
[OSComment] [nvarchar](200) NULL,
[ServerManufacturer] [nvarchar](50) NULL,
[ServerModel] [nvarchar](50) NULL,
[IP] [binary](4) NULL,
[Mask] [binary](4) NULL,
[Gateway] [binary](4) NULL,
[PrimaryDNS] [binary](4) NULL,
[SecondaryDNS] [binary](4) NULL,
[NetworkType] [nvarchar](20) NULL,
[NetworkComment] [nvarchar](200) NULL,
[SiteId] [uniqueidentifier] NULL,
[DataCenterRow] [smallint] NULL,
[DataCenterRack] [smallint] NULL,
[AntiVirusProduct] [nvarchar](50) NULL,
[AntiVirusVersion] [nvarchar](50) NULL,
[AntiVirusComment] [nvarchar](200) NULL,
[PassedOSSecurity] [nvarchar](50) NULL,
[Approved] [bit] NULL,
[ApprovedBy] [nvarchar](50) NULL,
CONSTRAINT [PK_Servers] PRIMARY KEY CLUSTERED
(
[ServerId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
In my C# Windows Forms application I have a form that displays the contents of this table and performs Display/Create/Delete/Update operation.
Now when I edit certain fields in a record I need to performs a query that will update only the fields which has been updated. In other words, I need a query that will dynamically identify the edited fields and update them.
The stored procedure that updates the columns one by one looks like this:
CREATE PROCEDURE dbo.UpdateServer #ServerName nvarchar(50), #HostName nvarchar(50),#OperatingSystem nvarchar(50),
#OSVersion nvarchar(50), #OSComment nvarchar(50),#ServerManufacturer nvarchar(50),
#ServerModel nvarchar(50), #IP nvarchar(15),#Mask nvarchar(15), #Gateway nvarchar(15),
#PrimaryDNS nvarchar(15), #SecondaryDNS nvarchar(15), #NetworkType nvarchar(20),#NetworkComment nvarchar(200),
#SiteId uniqueidentifier, #DataCenterRow smallint, #DataCenterRack smallint, #AntiVirusProduct nvarchar(50),
#AntiVirusVersion nvarchar(50), #AntiVirusComment nvarchar(200), #PassedOSSecurity bit, #Approved bit,
#ApprovedBy nvarchar(50)
AS
UPDATE [SystemsInfo].[dbo].[Servers]
SET ServerName=#ServerName,HostName=#HostName,OperatingSystem=#OperatingSystem,OSVersion=#OSVersion,OSComment=#OSComment,ServerManufacturer=#ServerManufacturer,ServerModel=#ServerModel,IP=(select dbo.fnBinaryIPv4(#IP)),
Mask=(select dbo.fnBinaryIPv4(#Mask)),Gateway=(select dbo.fnBinaryIPv4(#Gateway)),PrimaryDNS=(select dbo.fnBinaryIPv4(#PrimaryDNS)),SecondaryDNS=(select dbo.fnBinaryIPv4(#SecondaryDNS)),
NetworkType=#NetworkType,NetworkComment=#NetworkComment,SiteId=#SiteId,
DataCenterRow=#DataCenterRow, DataCenterRack=#DataCenterRack, AntiVirusProduct=#AntiVirusProduct, AntiVirusVersion=#AntiVirusVersion, AntiVirusComment=#AntiVirusComment, PassedOSSecurity=#PassedOSSecurity,
Approved=#Approved,ApprovedBy=#ApprovedBy;
I think the comment provided by Steve is the main way.
But, another option is to track the fields edited by user in your UI (eg. CellEndEdit or CellValueChanged), and create the dynamic sql query in your c# code (using a StringBuilder and SqlParameters) and finally execute it.
I would like to import\export file from Sharepoint 2013 database via C#. Do you know how to do it? Because file in SharePoint is encrypted.
My question is: How import\export file in C# from SharePoint Foundation 2013 database?
CREATE SCRIPT [dbo].[AllDocs]
CREATE TABLE [dbo].[AllDocs](
[Id] [uniqueidentifier] NOT NULL,
[SiteId] [uniqueidentifier] NOT NULL,
[DirName] [nvarchar](256) NOT NULL,
[LeafName] [nvarchar](128) NOT NULL,
[Level] [tinyint] NOT NULL,
[ParentId] [uniqueidentifier] NOT NULL,
[DeleteTransactionId] [varbinary](16) NOT NULL,
[WebId] [uniqueidentifier] NOT NULL,
[ListId] [uniqueidentifier] NULL,
[DoclibRowId] [int] NULL,
[Type] [tinyint] NOT NULL,
[SortBehavior] [tinyint] NOT NULL,
[Size] [int] NULL,
[ETagVersion] AS (case when [InternalVersion] IS NULL then NULL else ([InternalVersion]+[BumpVersion]*(256))/(256) end),
[EffectiveVersion] AS (case when [InternalVersion] IS NULL then NULL else [InternalVersion]+[BumpVersion]*(256) end),
[InternalVersion] [int] NULL,
[ContentVersion] [int] NOT NULL,
[NextBSN] [bigint] NULL,
[MetadataNextBSN] [bigint] NULL,
[StreamSchema] [tinyint] NULL,
[HasStream] AS (case when [Type]=(0) AND ([DocFlags]&(256))=(256) AND [SetupPath] IS NULL OR [SetupPath] IS NOT NULL AND ([DocFlags]&(64))=(64) then (1) else (0) end),
[BumpVersion] [tinyint] NOT NULL,
[UIVersion] [int] NOT NULL,
[Dirty] AS (case when [BumpVersion]<>(0) then CONVERT([bit],(1)) else CONVERT([bit],(0)) end),
[ListDataDirty] [bit] NOT NULL,
[DocFlags] [int] NULL,
[ThicketFlag] [bit] NULL,
[CharSet] [int] NULL,
[ProgId] [nvarchar](255) NULL,
[TimeCreated] [datetime] NOT NULL,
[TimeLastModified] [datetime] NOT NULL,
[NextToLastTimeModified] [datetime] NULL,
[MetaInfoTimeLastModified] [datetime] NULL,
[TimeLastWritten] [datetime] NULL,
[SetupPathVersion] [tinyint] NOT NULL,
[SetupPath] [nvarchar](255) NULL,
[SetupPathUser] [nvarchar](255) NULL,
[CheckoutUserId] [int] NULL,
[DraftOwnerId] [int] NULL,
[CheckoutDate] [datetime] NULL,
[CheckoutExpires] [datetime] NULL,
[VersionCreatedSinceSTCheckout] [bit] NOT NULL,
[LTCheckoutUserId] AS (case when ([DocFlags]&(32))=(32) then [CheckoutUserId] end),
[CheckinComment] [nvarchar](1023) NULL,
[IsCheckoutToLocal] AS (case when ([DocFlags]&(512))=(512) then (1) else (0) end),
[VirusVendorID] [int] NULL,
[VirusStatus] [tinyint] NULL,
[VirusInfo] [nvarchar](255) SPARSE NULL,
[VirusInfoEx] [varbinary](max) NULL,
[MetaInfo] [dbo].[tCompressedBinary] NULL,
[MetaInfoSize] [int] NULL,
[MetaInfoVersion] [int] NOT NULL,
[UnVersionedMetaInfo] [dbo].[tCompressedBinary] NULL,
[UnVersionedMetaInfoSize] [int] NULL,
[UnVersionedMetaInfoVersion] [int] NULL,
[WelcomePageUrl] [nvarchar](260) NULL,
[WelcomePageParameters] [nvarchar](max) NULL,
[IsCurrentVersion] [bit] NOT NULL,
[AuditFlags] [int] NULL,
[InheritAuditFlags] [int] NULL,
[UIVersionString] AS ((CONVERT([nvarchar],[UIVersion]/(512))+'.')+CONVERT([nvarchar],[UIVersion]%(512))),
[ScopeId] [uniqueidentifier] NOT NULL,
[BuildDependencySet] [varbinary](max) NULL,
[ParentVersion] [int] NULL,
[ParentVersionString] AS ((CONVERT([nvarchar],[ParentVersion]/(512))+'.')+CONVERT([nvarchar],[ParentVersion]%(512))),
[TransformerId] [uniqueidentifier] NULL,
[ParentLeafName] [nvarchar](128) NULL,
[CtoOffset] [smallint] NULL,
[Extension] AS (case when charindex(N'.',[LeafName] collate Latin1_General_BIN)>(0) then right([LeafName],charindex(N'.',reverse([LeafName]) collate Latin1_General_BIN)-(1)) else N'' end),
[ExtensionForFile] AS (case when [Type]=(0) AND charindex(N'.',[LeafName] collate Latin1_General_BIN)>(0) then right([LeafName],charindex(N'.',reverse([LeafName]) collate Latin1_General_BIN)-(1)) else N'' end),
[ItemChildCount] [int] NOT NULL,
[FolderChildCount] [int] NOT NULL,
[FileFormatMetaInfo] [varbinary](max) NULL,
[FileFormatMetaInfoSize] [int] NOT NULL,
[FFMConsistent] [bit] NULL,
[ListSchemaVersion] [int] NULL,
[ClientId] [varbinary](16) NULL,
CONSTRAINT [AllDocs_ParentId] PRIMARY KEY CLUSTERED
(
[SiteId] ASC,
[DeleteTransactionId] ASC,
[ParentId] ASC,
[Id] ASC,
[Level] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = ON, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 95) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
I try use this:
1 Code:
-- To allow advanced options to be changed.
EXEC sp_configure 'show advanced options', 1
GO
-- To update the currently configured value for advanced options.
RECONFIGURE
GO
-- To enable the feature.
EXEC sp_configure 'xp_cmdshell', 1
GO
-- To update the currently configured value for this feature.
RECONFIGURE
GO
2 Code
DECLARE #Command VARCHAR(4000),
#FileID VARCHAR(128),
#MyFilename nvarchar(max)
DECLARE curFile CURSOR FOR -- Cursor for each image in table
SELECT DocId FROM [WSS_Content].[dbo].[DocStreams]
OPEN curFile
FETCH NEXT FROM curFile
INTO #FileID
WHILE (##FETCH_STATUS = 0) -- Cursor loop
BEGIN
-- Keep the bcp command on ONE LINE - SINGLE LINE!!! - broken up for presentation
SELECT #MyFilename = LeafName from [WSS_Content].[dbo].[AllDocs] WHERE Id = #FileID
SET #Command = 'bcp "SELECT Content from [WSS_Content].[dbo].[DocStreams] WHERE DocId = ''' +
#FileID + '''" queryout "C:\' +
#MyFilename+'" -T -n -Slocalhost -fC:\bcp.fmt'
PRINT #Command -- debugging
EXEC xp_cmdshell #Command -- Carry out image export to file from db table
FETCH NEXT FROM curFile
INTO #FileID
END -- cursor loop
CLOSE curFile
DEALLOCATE curFile
But I get error:
It`s simple to import\export file in C# because it's binary file SELECT Content from [WSS_Content].[dbo].[DocStreams]
I wanted to retrieve the name of the employee from one table and the details of their leave application from another table. The condition that differentiates the employee from his reporting boss is the manager_ID column. The employee_ID is fed as value of manager_ID column for each user. The director has null in this field. Now I have populated this data in a gridview using the below query.
SqlConnection conn = new SqlConnection(connectionstring);
SqlCommand cmd = new SqlCommand("select distinct Employee.emp_name,Leave.leave_ID,Leave.leave_type,Leave.reason,Leave.el_start_date,Leave.el_end_date,Leave.total_days,Leave.status from Employee CROSS JOIN Leave where Employee.manager_ID='" + lbl_empid.Text + "' AND Leave.status='" + lbl_status.Text + "'", conn);
cmd.CommandType = CommandType.Text;
using (conn)
{
conn.Open();
rdr = cmd.ExecuteReader();
while (rdr.Read())
{
dr = dt.NewRow();
dr["Leave No."] = rdr["leave_ID"].ToString();
dr["Requested By"] = rdr["emp_name"].ToString();
dr["Type of Leave"] = rdr["leave_type"].ToString();
dr["Reason"] = rdr["reason"].ToString();
dr["Starting"] = rdr["el_start_date"].ToString();
dr["Ending"] = rdr["el_end_date"].ToString();
dr["No. of Days"] = rdr["total_days"].ToString();
dr["Status"] = rdr["status"].ToString();
dt.Rows.Add(dr);
dt.AcceptChanges();
}
gv_pending_requests.DataSource = dt;
gv_pending_requests.DataBind();
conn.Close();
}
On debugging, the gridview gets populated without an error. It shows the employees managed by the logged in manager, all right. Only those employees' rows are retrieved and displayed that were required to be. But the data is duplicated in a strange manner. The same row is repeated with a replaced name in the output. I have made a crude imitation of the output I get in my gridview, as I am not allowed to post images. The output shows the data for all the users that have the same manager, by repeating it. While in reality I made these requests from the account of Avneesh only, not ravi:
Lv. No. Employee Name LeaveType Reason Starting Date
2 Avneesh Earned, Casual Personal 17/11/2014
2 Ravi Earned, Casual Personal 17/11/2014
5 Avneesh Earned, Half-Pay Conference 17/11/2014
5 Ravi Earned, Half-Pay Conference 17/11/2014
Please point out what I am doing wrong in the query. I have tried using a self join in a subquery, but it shows the error:- {"System.Data.SqlClient.SqlException (0x80131904): Incorrect syntax near the keyword 'where'"}. The query is:
SqlCommand cmd = new SqlCommand("select distinct Employee.emp_name,Leave.leave_ID,Leave.leave_type,Leave.reason,Leave.el_start_date,Leave.el_end_date,Leave.total_days,Leave.status from Employee CROSS JOIN Leave where Employee.manager_ID=(select e.emp_ID,m.emp_ID from Employee e INNER JOIN Employee m where e.mgr_ID=m.emp_ID) AND Leave.status='" + lbl_status.Text + "'", conn);
Now, how do i solve this duplication? Please help.
I am using Visual studio 2013 and sql server 2014.
Edit: Here are the two tables concerned with this query.
CREATE TABLE [dbo].[Employee](
[emp_ID] [numeric](18, 0) IDENTITY(1,1) NOT NULL,
[username] [nvarchar](50) NOT NULL,
[emp_name] [nvarchar](max) NOT NULL,
[password] [nvarchar](max) NOT NULL,
[pay_roll_num] [nvarchar](max) NOT NULL,
[designation] [nvarchar](max) NOT NULL,
[leaves_taken] [nvarchar](max) NOT NULL,
[primary_address] [nvarchar](max) NOT NULL,
[primary_phone] [nvarchar](max) NOT NULL,
[email_ID] [nvarchar](max) NOT NULL,
[manager_ID] [nvarchar](max) NOT NULL,
CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED ( [username] 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]
CREATE TABLE [dbo].[Leave](
[leave_ID] [numeric](18, 0) IDENTITY(1,1) NOT NULL,
[username] [nvarchar](50) NOT NULL,
[reason] [nvarchar](max) NOT NULL,
[addrs_onLeave] [nvarchar](max) NOT NULL,
[phone_onLeave] [nvarchar](max) NOT NULL,
[alternate_email] [nvarchar](max) NULL,
[leave_type] [nvarchar](max) NOT NULL,
[earned_leave] [nvarchar](max) NULL,
[el_start_date] [nvarchar](max) NULL,
[el_end_date] [nvarchar](max) NULL,
[halfpay_leave] [nvarchar](max) NULL,
[hl_start_date] [nvarchar](max) NULL,
[hl_end_date] [nvarchar](max) NULL,
[commuted_leave] [nvarchar](max) NULL,
[com_startdate] [nvarchar](max) NULL,
[com_end_date] [nvarchar](max) NULL,
[casual_leave] [nvarchar](max) NULL,
[cl_start_date] [nvarchar](max) NULL,
[cl_end_date] [nvarchar](max) NULL,
[rh_leave] [nvarchar](max) NULL,
[rh_start_date] [nvarchar](max) NULL,
[rh_end_date] [nvarchar](max) NULL,
[total_days] [nvarchar](max) NOT NULL,
[status] [nvarchar](max) NOT NULL,
[leave_apply_date] [nvarchar](max) NOT NULL,
CONSTRAINT [PK_Leave] PRIMARY KEY CLUSTERED ( [leave_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 agree the CROSS JOIN is the issue.
It is rare to need to CROSS JOIN to a table or query set when that table or query set has multiple rows, thereby creating a cartesian product. On the other hand, I CROSS JOIN to a table or query that has a SINGLE row in it often, but doing so does not increase the number of rows in the result set, and thereby does not duplicate rows.
A good example to conceptualize when to use a CROSS JOIN to a multiple row table or set, is a batch process that needs to post debits and credits (double entry bookkeeping). You can join to a query that contains two rows, one containing 'DEBIT', the other 'CREDIT', and doing so has just doubled all rows in the batch. One half gets posted as a DEBIT, the other half as a CREDIT.
Solved it! Thank you all for suggesting to get rid of the cross join.
Here is the right query, in case anyone else might need it.
SqlCommand cmd = new SqlCommand("select distinct leave_ID,username,leave_type,reason,
el_start_date,el_end_date,total_days,status from Leave where username IN
(select username from Employee where manager_ID='" + lbl_empid.Text + "')
AND Leave.status='" + lbl_deny.Text + "'", conn);
I have some inputs as
CREATE PROCEDURE dbo._ws_CallLogs_DeleteAll
(
#UserId uniqueidentifier
)
AS
OR
/*========== Script Analyzed by Sql Eye on 11/30/2012 2:55:12 PM =======
*====================== Total warnings : 0 =================================== */
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
SET ANSI_PADDING ON
GO
IF EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_CachedPlan_cached_plan_Job_id]')
AND parent_object_id = OBJECT_ID(N'[dbo].[CachedPlan]'))
ALTER TABLE [dbo].[CachedPlan] DROP CONSTRAINT [FK_CachedPlan_cached_plan_Job_id]
GO
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CachedPlan]') AND TYPE IN (N'U'))
DROP TABLE [dbo].[CachedPlan]
GO
CREATE TABLE [dbo].[CachedPlan](
[cached_plan_id] [int] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
[cached_plan_Job_id] [int] NOT NULL,
[dbId] [int] NOT NULL,
[dbname] [varchar](100) NOT NULL,
[plan_type] [varchar](50) NOT NULL,
[objId] [int] NOT NULL,
[objname] [varchar](100) NOT NULL,
[sql_batch] [varchar](max) NOT NULL,
I need to pick out
PROCEDURE dbo._ws_CallLogs_DeleteAll
OR
TABLE [dbo].[CachedPlan]
What is the most efficient way to do so?
Use a regular expression:
Match m = Regex.Match(inputString, #"CREATE\s+(?<obj>.+?)\s*\(", RegexOptions.Singleline);
string objectName = m.Groups["obj"].Value;