SELECT CURRENT_TIMESTAMPor
SELECT {fn NOW()}orSELECT GETDATE()
Select * From sysobjects Where type = 'U' Order By nameChange the type to 'P' and you will get all the Stored Procedures.
Select * From sys.tablesHow to get the content of stored procedures?
Select text From syscomments Where id = (Select id From sysobjects Where type='P' and name = 'SprocName')or
Exec sp_helptext SprocNameI prefer the second one.
Create PROCEDURE [dbo].[getMemberInfo] ( @pageNumber int = 1, --which page you want to fetch @rowsPerPage int = 50 --how many rows per page ) AS BEGIN SET NOCOUNT ON; Declare @startRowIndex int, @endRowIndex int Set @startRowIndex = ((@pageNumber - 1) * @rowsPerPage) + 1 Set @endRowIndex = @startRowIndex + @rowsPerPage - 1 Select * From ( Select *, row_number() Over (Order By accountName) As rownum From dbo.Member ) Where rownum Between @startRowIndex And @endRowIndex End GO
Select Name, IsNull(Age, 18) From Members2.Use the following simple method
public DataTable RemoveDBNull(DataTable dt)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
for (int j = 0; j < dt.Columns.Count; j++)
{
if (dt.Rows[i][j] == DBNull.Value)
dt.Rows[i][j] = 0;
}
}
return dt;
}
private DataSet DBConnectionForGettingData(string sprocName, Dictionary<string, string> paramList)
{
using (SqlConnection conn = new SqlConnection(str_ConnectionString))
{
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = sprocName;
cmd.Connection = conn;
//loop through the dictionary
foreach (string key in paramList.Keys)
{
cmd.Parameters.AddWithValue(key, paramList[key]);
}
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
//release the resource
cmd = null;
da = null;
return ds;
}
}The paramList is the collection of parameters. All you need to do is create a Dictionary object and add all the parameters to this object. You can also adjust the arguments of this method to make it more flexible.
Begin Try
Update xxxx
Set yy = zzz
Where aa = 123
Select @updateRowcount = @@Rowcount, @ErrorCode = @@Error
If (@updateRowcount = 0)
Raiserror('0 row(s) affected', 11, 1)
If (@ErrorCode != 0)
Raiserror('Update failed.', 11, 1)
End Try
Begin Catch
Select Error_number(), Error_line(), Error_message()
return
End CatchThe string in the Raiserror() function is the "specific message".
//This is a new feature of C# 3.0. Take a look of this:
//"http://blogs.msdn.com/abhinaba/archive/2005/09/17/470358.aspx".
List<int> CustomerIdList = new List<int>{1001,1002,1003,1004};
string CustomerIds = MergeData(CustomerIdList);Create your own MergeData() method to merge the input List<int> and output as a single string like this: "1001,1002,1003,1004".CREATE FUNCTION [dbo].[fxnParseCommaDelmitedList]
(
@CommaDelimitedList varchar(8000)
)
RETURNS @TableVar TABLE (ItemID int NOT NULL )
AS
BEGIN
DECLARE @IDListPosition int
DECLARE @IDList varchar(4000)
DECLARE @ArrValue varchar(4000)
SET @IDList = COALESCE(@CommaDelimitedList, '')
IF @IDList <> ''
BEGIN
-- Add comma to end of list so user doesn''t have to
SET @IDList = @IDList + ','
-- Loop through the comma demlimted string list
WHILE PATINDEX('%,%' , @IDList ) <> 0
BEGIN
SELECT @IDListPosition = PATINDEX('%,%' , @IDList)
SELECT @ArrValue = LEFT(@IDList, @IDListPosition - 1)
-- Insert parsed ID into TableVar for "where in select"
INSERT INTO @TableVar (ItemID) VALUES (CONVERT(int, @ArrValue))
-- Remove processed string
SELECT @IDList = STUFF(@IDList, 1, @IDListPosition, '')
END
END
RETURN
ENDItemID (column name) 1001 1002 1003 1004
DECLARE @tempTable TABLE (
userId INT,
userName NVARCHAR(50),
password NVARCHAR(50)
)
DECLARE @xml XML
SET @xml='
<row userId="67" userName="Kenny1" password="1234" />
<row userId="80" userName="Kenny2" password="5678" />'
INSERT INTO @tempTable
SELECT Tbl.Col.value('@userId', 'INT'),
Tbl.Col.value('@userName', 'NVARCHAR(50)'),
Tbl.Col.value('@password', 'NVARCHAR(50)')
FROM @xml.nodes('//row') Tbl(Col)
--See the table
SELECT * FROM @tempTable
CREATE PROCEDURE [dbo].[SearchExample] (
@userId UNIQUEIDENTIFIER = NULL, --set all parameters' default value to null
@firstName NVARCHAR(50) = NULL,
@lastName NVARCHAR(50) = NULL,
@address NVARCHAR(100) = NULL,
@zipcode INT = NULL
)
SELECT ... --omit
FROM dbo.UserInfo u
WHERE u.userId = COALESCE(@userId, u.userId)
AND u.firstName LIKE ('%'+ COALESCE(@firstName, u.firstName)+'%')
AND ... --omit
... --omitSet the default value to null for all the input parameters is because if we don't have any filter to search (the value is null), then COALESCE() will use the second parameter as the output, otherwise it will output the first parameter.WHERE u.userId = COALESCE(@userId, u.userId)will become like
WHERE u.userId = 1001Otherwise, it will become like
WHERE u.userId = u.userId