阿青

 
 

Powered by: 博客园
模板提供:沪江博客
博客园 | 首页 | 发新随笔 | 发新文章 | 联系 | 订阅订阅 | 管理

2011年3月21日

MSSQL中,使用T-SQL脚本获取上周一到上周五的数据

  在MSSQL中,我们要查询上周一到上周五的数据,而上周的日期不是固定的,也就是今天可能是1月1号,可能是10月1号,而我们就是要获取上周一至周五的数据,那么首先想到一个思路,先获取上周一的日期在获取上周五的日期

 
   上周一的日期怎么获取呢,首先可以用T-SQL中的datepart获取当前周几,然后通过dateadd函数减掉计算的天数即可。


    注意:SQL中默认第一天是周天,我们可以用set datefirst 1 设置每周一为第一天。

 那么我们假设今天周三,上周一的日期,公式为:今天 - {周三(3)} - 6
    上周五的日期,公式为:今天 - {周三(3)} - 2

好了,既然上周一和上周五的日期我们已经获取到了,select一下就大功告成了,T-SQL如下:

SET DATEFIRST 1
DECLARE @day1 DATETIME
DECLARE @dat5temp DATETIME
DECLARE @day5 VARCHAR(100)
SET @day1 = convert(char(10),dateadd(dd, - DATEPART (weekday , getdate())-6,getdate()),120)
SET @dat5temp = convert(char(10),dateadd(dd, - DATEPART (weekday , getdate())-2,getdate()),120)
SET @day5 = CAST(YEAR(@dat5temp) AS VARCHAR(4)) + '-' + CAST(MONTH(@dat5temp) AS VARCHAR(2))
+ '-' + CAST(DAY(@dat5temp) AS VARCHAR(2)) + ' 23:59:59'
PRINT @dat5temp
PRINT @day5
SELECT * FROM dbo.News WHERE PublishDate BETWEEN @day1 AND @day5
posted @ 2011-03-21 14:07 阿青 阅读(437) 评论(2) 编辑
 
MSSQL,通过T-SQL脚本查询表的所有外键

   当我们要对两个数据库的相关数据进行完全同步的时候,我们首先是先删除数据,然后再添加数据,这样保证所有数据的一致性,

那么假设现在我们需要从源数据库中的表A同步到目标数据库中的表A,脚本如下

1.delete  targetDBName.dbo.TBA   

2.insert into targetDBName.dbo.TBA select * from sourceDBName.dbo.TBA  (动态字段可以查询syscolumns表来实现)

那么在执行第一句脚本的时候,就有可能报错,因为TBA有可能存在外键关联。

  在这种的情况下,

    删除操作:先删子表在删主表

    添加操作:先添主表在添子表

   因此我们需要对TBA的所有外键进行排查,脚本如下:

--查询外键约束
select FK_Name as [外键名],Parent_Tab_Name as [外键表],
[外键列]=stuff((select ','+[Parent_Col_Name] from (
select FK.name as FK_Name,Parent_Tab.Name as Parent_Tab_Name,Parent_Col.Name as Parent_Col_Name,
Referenced_Tab.Name
as Referenced_Tab_Name,Referenced_Col.Name as Referenced_Col_Name
from sys.foreign_keys FK
inner join sys.foreign_key_columns Col on FK.Object_ID = Col.constraint_object_id
inner join sys.objects Parent_Tab ON Col.parent_object_id = Parent_Tab.Object_ID and Parent_Tab.TYPE = 'U'
inner join sys.columns Parent_Col on Parent_Tab.Object_ID = Parent_Col.object_id
and Col.parent_column_id = Parent_Col.column_id
inner join sys.objects Referenced_Tab ON Col.referenced_object_id = Referenced_Tab.Object_ID
        and Referenced_Tab.TYPE = 'U'
inner join sys.columns Referenced_Col on Referenced_Tab.Object_ID = Referenced_Col.object_id
and Col.referenced_column_id = Referenced_Col.column_id
)t
where FK_Name=tb.FK_Name and Parent_Tab_Name = tb.Parent_Tab_Name
    and Referenced_Tab_Name = tb.Referenced_Tab_Name for xml path('')), 1, 1, ''),
Referenced_Tab_Name
as [主键表],
[主键列]=stuff((select ','+[Referenced_Col_Name] from (
select FK.name as FK_Name,Parent_Tab.Name as Parent_Tab_Name,Parent_Col.Name as Parent_Col_Name,
Referenced_Tab.Name
as Referenced_Tab_Name,Referenced_Col.Name as Referenced_Col_Name
from sys.foreign_keys FK
inner join sys.foreign_key_columns Col on FK.Object_ID = Col.constraint_object_id
inner join sys.objects Parent_Tab ON Col.parent_object_id = Parent_Tab.Object_ID and Parent_Tab.TYPE = 'U'
inner join sys.columns Parent_Col on Parent_Tab.Object_ID = Parent_Col.object_id
and Col.parent_column_id = Parent_Col.column_id
inner join sys.objects Referenced_Tab ON Col.referenced_object_id = Referenced_Tab.Object_ID
        and Referenced_Tab.TYPE = 'U'
inner join sys.columns Referenced_Col on Referenced_Tab.Object_ID = Referenced_Col.object_id
and Col.referenced_column_id = Referenced_Col.column_id
)t
where FK_Name=tb.FK_Name and Parent_Tab_Name = tb.Parent_Tab_Name
    and Referenced_Tab_Name = tb.Referenced_Tab_Name for xml path('')), 1, 1, '')
--as [外键列]
from (
select FK.name as FK_Name,Parent_Tab.Name as Parent_Tab_Name,Parent_Col.Name as Parent_Col_Name,
Referenced_Tab.Name
as Referenced_Tab_Name,Referenced_Col.Name as Referenced_Col_Name
from sys.foreign_keys FK
inner join sys.foreign_key_columns Col on FK.Object_ID = Col.constraint_object_id
inner join sys.objects Parent_Tab ON Col.parent_object_id = Parent_Tab.Object_ID and Parent_Tab.TYPE = 'U'
inner join sys.columns Parent_Col on Parent_Tab.Object_ID = Parent_Col.object_id
and Col.parent_column_id = Parent_Col.column_id
inner join sys.objects Referenced_Tab ON Col.referenced_object_id = Referenced_Tab.Object_ID
      and Referenced_Tab.TYPE = 'U'
inner join sys.columns Referenced_Col on Referenced_Tab.Object_ID = Referenced_Col.object_id
and Col.referenced_column_id = Referenced_Col.column_id
)tb
--WHERE tb.Parent_Tab_Name='PB_UserRole' OR tb.Referenced_Tab_Name='PB_UserRole'
group by FK_Name,Parent_Tab_Name,Referenced_Tab_Name
posted @ 2011-03-21 10:42 阿青 阅读(170) 评论(0) 编辑
 

2011年2月23日

在ASP.NET WebForm简单实现伪静态,通过(System.Web.Routing)

开发环境:VS2010  

   VS2010已经集成了Routing组件,在ASP.NET MVC中,我们通过URLRouting实现了Controller,Action的URL控制。在WEBForm中,同样可以!马上开始。

首先,打开VS2010新建一个VS2010webForm,命名为(UrlTest),首先我们为网站添加System.Web.Routing引用,如图

好,引用添加好了。

第二步,我们在Global.asax中添加,RouteCollection的注册,关键代码如下:

注:先Global.asax中添加一下引用<%@ Import Namespace="System.Web.Routing" %>

void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute(
"DefaultRoute",
"Default/{id}.html",
"~/Default.aspx");
}

void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
// 在应用程序启动时运行的代码

}

现在我们已经注册了一个Route,对Default/{id}.html,映射到Default.aspx,这样我们在Default.aspx只要通过RouteData对象,获取值,键为id的参数即可。

那么在Default.aspx.cs里面我们写入以下代码:

if (RouteData.Values["id"] != null)
{
Response.Write(
"<h1>" + RouteData.Values["id"].ToString() + "</h1>");
}

好了,简单吧!运行程序输入Url  http://localhost:9801/UrlTest/Default/123.html

效果如图所示:

看到URL地址了吧,还有页面左上角的”123“了吧。

当然要发布到IIS,记得配置下映射哦。

posted @ 2011-02-23 17:32 阿青 阅读(335) 评论(3) 编辑